blob: 340f48fc6b207d01ddb65c2fccddf6c820ad163e [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>
Mark Salyzyn7823e122016-09-29 08:08:05 -070021#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070022#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070023#include <system/window.h>
24#include <ui/BufferQueueDefs.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080025#include <utils/StrongPointer.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080026#include <utils/Trace.h>
Brian Anderson1049d1d2016-12-16 17:25:57 -080027#include <utils/Vector.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070028
29#include <algorithm>
30#include <unordered_set>
31#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070032
Chia-I Wu4a6a9162016-03-26 07:17:34 +080033#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070034
Daniel Kochf25f5bb2017-10-05 00:26:58 -040035using android::hardware::graphics::common::V1_0::BufferUsage;
36
Chia-I Wu62262232016-03-26 07:06:44 +080037namespace vulkan {
38namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070039
Jesse Halld7b994a2015-09-07 14:17:37 -070040namespace {
41
Jesse Hall55bc0972016-02-23 16:43:29 -080042const VkSurfaceTransformFlagsKHR kSupportedTransforms =
43 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
44 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
45 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
46 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070047 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
48 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
49 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
50 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080051 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
52
53VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
54 // Native and Vulkan transforms are isomorphic, but are represented
55 // differently. Vulkan transforms are built up of an optional horizontal
56 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
57 // transforms are built up from a horizontal flip, vertical flip, and
58 // 90-degree rotation, all optional but always in that order.
59
Jesse Hall55bc0972016-02-23 16:43:29 -080060 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070061 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080062 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070063 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
64 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
65 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
66 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
67 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080068 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070069 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080070 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070071 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
72 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
73 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
74 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
75 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -080076 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
77 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
78 default:
79 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
80 }
81}
82
Yiwei Zhang70a21962019-05-31 17:26:52 -070083int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
84 switch (transform) {
85 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
86 return NATIVE_WINDOW_TRANSFORM_ROT_90;
87 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
88 return NATIVE_WINDOW_TRANSFORM_ROT_180;
89 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
90 return NATIVE_WINDOW_TRANSFORM_ROT_270;
91 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
92 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
93 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
94 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
95 NATIVE_WINDOW_TRANSFORM_ROT_90;
96 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
97 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
98 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
99 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
100 NATIVE_WINDOW_TRANSFORM_ROT_90;
101 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
102 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
103 default:
104 return 0;
105 }
106}
107
Jesse Hall178b6962016-02-24 15:39:50 -0800108int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
109 switch (transform) {
110 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
111 return NATIVE_WINDOW_TRANSFORM_ROT_270;
112 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
113 return NATIVE_WINDOW_TRANSFORM_ROT_180;
114 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
115 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700116 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
117 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
118 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
119 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
120 NATIVE_WINDOW_TRANSFORM_ROT_90;
121 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
122 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
123 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
124 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
125 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800126 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
127 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
128 default:
129 return 0;
130 }
131}
132
Ian Elliott8a977262017-01-19 09:05:58 -0700133class TimingInfo {
134 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800135 TimingInfo() = default;
136 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),
232 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
233 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700234 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700235 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700236 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700237 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700238 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600239 uint64_t get_refresh_duration()
240 {
241 ANativeWindow* window = surface.window.get();
242 native_window_get_refresh_cycle_duration(
243 window,
244 &refresh_duration);
245 return static_cast<uint64_t>(refresh_duration);
246
247 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800248
249 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700250 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700251 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700252 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700253 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700254 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300255 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700256
257 struct Image {
258 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
259 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800260 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700261 // The fence is only valid when the buffer is dequeued, and should be
262 // -1 any other time. When valid, we own the fd, and must ensure it is
263 // closed: either by closing it explicitly when queueing the buffer,
264 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
265 int dequeue_fence;
266 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800267 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700268
Brian Anderson1049d1d2016-12-16 17:25:57 -0800269 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700270};
271
272VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
273 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
274}
275
276Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800277 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700278}
279
Jesse Halldc225072016-05-30 22:40:14 -0700280void ReleaseSwapchainImage(VkDevice device,
281 ANativeWindow* window,
282 int release_fence,
283 Swapchain::Image& image) {
284 ALOG_ASSERT(release_fence == -1 || image.dequeued,
285 "ReleaseSwapchainImage: can't provide a release fence for "
286 "non-dequeued images");
287
288 if (image.dequeued) {
289 if (release_fence >= 0) {
290 // We get here from vkQueuePresentKHR. The application is
291 // responsible for creating an execution dependency chain from
292 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
293 // (release_fence), so we can drop the dequeue_fence here.
294 if (image.dequeue_fence >= 0)
295 close(image.dequeue_fence);
296 } else {
297 // We get here during swapchain destruction, or various serious
298 // error cases e.g. when we can't create the release_fence during
299 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
300 // have already signalled, since the swapchain images are supposed
301 // to be idle before the swapchain is destroyed. In error cases,
302 // there may be rendering in flight to the image, but since we
303 // weren't able to create a release_fence, waiting for the
304 // dequeue_fence is about the best we can do.
305 release_fence = image.dequeue_fence;
306 }
307 image.dequeue_fence = -1;
308
309 if (window) {
310 window->cancelBuffer(window, image.buffer.get(), release_fence);
311 } else {
312 if (release_fence >= 0) {
313 sync_wait(release_fence, -1 /* forever */);
314 close(release_fence);
315 }
316 }
317
318 image.dequeued = false;
319 }
320
321 if (image.image) {
322 GetData(device).driver.DestroyImage(device, image.image, nullptr);
323 image.image = VK_NULL_HANDLE;
324 }
325
326 image.buffer.clear();
327}
328
329void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
330 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
331 return;
Jesse Halldc225072016-05-30 22:40:14 -0700332 for (uint32_t i = 0; i < swapchain->num_images; i++) {
333 if (!swapchain->images[i].dequeued)
334 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
335 }
336 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700337 swapchain->timing.clear();
338}
339
340uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800341 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
342 return 0;
343 }
Ian Elliott8a977262017-01-19 09:05:58 -0700344
Brian Anderson1049d1d2016-12-16 17:25:57 -0800345 uint32_t num_ready = 0;
346 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
347 for (uint32_t i = 0; i < num_timings; i++) {
348 TimingInfo& ti = swapchain.timing.editItemAt(i);
349 if (ti.ready()) {
350 // This TimingInfo is ready to be reported to the user. Add it
351 // to the num_ready.
352 num_ready++;
353 continue;
354 }
355 // This TimingInfo is not yet ready to be reported to the user,
356 // and so we should look for any available timestamps that
357 // might make it ready.
358 int64_t desired_present_time = 0;
359 int64_t render_complete_time = 0;
360 int64_t composition_latch_time = 0;
361 int64_t actual_present_time = 0;
362 // Obtain timestamps:
363 int ret = native_window_get_frame_timestamps(
364 swapchain.surface.window.get(), ti.native_frame_id_,
365 &desired_present_time, &render_complete_time,
366 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700367 nullptr, //&first_composition_start_time,
368 nullptr, //&last_composition_start_time,
369 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800370 // TODO(ianelliott): Maybe ask if this one is
371 // supported, at startup time (since it may not be
372 // supported):
373 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700374 nullptr, //&dequeue_ready_time,
375 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800376
377 if (ret != android::NO_ERROR) {
378 continue;
379 }
380
381 // Record the timestamp(s) we received, and then see if this TimingInfo
382 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700383 ti.timestamp_desired_present_time_ = desired_present_time;
384 ti.timestamp_actual_present_time_ = actual_present_time;
385 ti.timestamp_render_complete_time_ = render_complete_time;
386 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800387
388 if (ti.ready()) {
389 // The TimingInfo has received enough timestamps, and should now
390 // use those timestamps to calculate the info that should be
391 // reported to the user:
392 ti.calculate(swapchain.refresh_duration);
393 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700394 }
395 }
396 return num_ready;
397}
398
399// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
400void copy_ready_timings(Swapchain& swapchain,
401 uint32_t* count,
402 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800403 if (swapchain.timing.empty()) {
404 *count = 0;
405 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700406 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800407
408 size_t last_ready = swapchain.timing.size() - 1;
409 while (!swapchain.timing[last_ready].ready()) {
410 if (last_ready == 0) {
411 *count = 0;
412 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700413 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800414 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700415 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800416
417 uint32_t num_copied = 0;
418 size_t num_to_remove = 0;
419 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
420 const TimingInfo& ti = swapchain.timing[i];
421 if (ti.ready()) {
422 ti.get_values(&timings[num_copied]);
423 num_copied++;
424 }
425 num_to_remove++;
426 }
427
428 // Discard old frames that aren't ready if newer frames are ready.
429 // We don't expect to get the timing info for those old frames.
430 swapchain.timing.removeItemsAt(0, num_to_remove);
431
Ian Elliott8a977262017-01-19 09:05:58 -0700432 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700433}
434
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700435android_pixel_format GetNativePixelFormat(VkFormat format) {
436 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
437 switch (format) {
438 case VK_FORMAT_R8G8B8A8_UNORM:
439 case VK_FORMAT_R8G8B8A8_SRGB:
440 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
441 break;
442 case VK_FORMAT_R5G6B5_UNORM_PACK16:
443 native_format = HAL_PIXEL_FORMAT_RGB_565;
444 break;
445 case VK_FORMAT_R16G16B16A16_SFLOAT:
446 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
447 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800448 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700449 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
450 break;
451 default:
452 ALOGV("unsupported swapchain format %d", format);
453 break;
454 }
455 return native_format;
456}
457
458android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
459 switch (colorspace) {
460 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
461 return HAL_DATASPACE_V0_SRGB;
462 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
463 return HAL_DATASPACE_DISPLAY_P3;
464 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
465 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600466 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
467 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700468 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
469 return HAL_DATASPACE_DCI_P3_LINEAR;
470 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
471 return HAL_DATASPACE_DCI_P3;
472 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
473 return HAL_DATASPACE_V0_SRGB_LINEAR;
474 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
475 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600476 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
477 return HAL_DATASPACE_BT2020_LINEAR;
478 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700479 return static_cast<android_dataspace>(
480 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
481 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600482 case VK_COLOR_SPACE_DOLBYVISION_EXT:
483 return static_cast<android_dataspace>(
484 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
485 HAL_DATASPACE_RANGE_FULL);
486 case VK_COLOR_SPACE_HDR10_HLG_EXT:
487 return static_cast<android_dataspace>(
488 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
489 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700490 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
491 return static_cast<android_dataspace>(
492 HAL_DATASPACE_STANDARD_ADOBE_RGB |
493 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
494 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
495 return HAL_DATASPACE_ADOBE_RGB;
496
497 // Pass through is intended to allow app to provide data that is passed
498 // to the display system without modification.
499 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
500 return HAL_DATASPACE_ARBITRARY;
501
502 default:
503 // This indicates that we don't know about the
504 // dataspace specified and we should indicate that
505 // it's unsupported
506 return HAL_DATASPACE_UNKNOWN;
507 }
508}
509
Jesse Halld7b994a2015-09-07 14:17:37 -0700510} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700511
Jesse Halle1b12782015-11-30 11:27:32 -0800512VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800513VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800514 VkInstance instance,
515 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
516 const VkAllocationCallbacks* allocator,
517 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800518 ATRACE_CALL();
519
Jesse Hall1f91d392015-12-11 16:28:44 -0800520 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800521 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800522 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
523 alignof(Surface),
524 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800525 if (!mem)
526 return VK_ERROR_OUT_OF_HOST_MEMORY;
527 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700528
Chia-I Wue8e689f2016-04-18 08:21:31 +0800529 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700530 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700531 int err = native_window_get_consumer_usage(surface->window.get(),
532 &surface->consumer_usage);
533 if (err != android::NO_ERROR) {
534 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
535 strerror(-err), err);
536 surface->~Surface();
537 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700538 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700539 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700540
Yiwei Zhang6435b322018-05-08 11:12:17 -0700541 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800542 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
543 if (err != 0) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800544 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
545 err);
546 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800547 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700548 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800549 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700550
Jesse Hall1356b0d2015-11-23 17:24:58 -0800551 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700552 return VK_SUCCESS;
553}
554
Jesse Halle1b12782015-11-30 11:27:32 -0800555VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800556void DestroySurfaceKHR(VkInstance instance,
557 VkSurfaceKHR surface_handle,
558 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800559 ATRACE_CALL();
560
Jesse Hall1356b0d2015-11-23 17:24:58 -0800561 Surface* surface = SurfaceFromHandle(surface_handle);
562 if (!surface)
563 return;
564 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700565 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700566 "destroyed VkSurfaceKHR 0x%" PRIx64
567 " has active VkSwapchainKHR 0x%" PRIx64,
568 reinterpret_cast<uint64_t>(surface_handle),
569 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800570 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800571 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800572 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800573 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800574}
575
Jesse Halle1b12782015-11-30 11:27:32 -0800576VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800577VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
578 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700579 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800580 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800581 ATRACE_CALL();
582
Yiwei Zhang6435b322018-05-08 11:12:17 -0700583 const Surface* surface = SurfaceFromHandle(surface_handle);
584 if (!surface) {
585 return VK_ERROR_SURFACE_LOST_KHR;
586 }
587 const ANativeWindow* window = surface->window.get();
588
589 int query_value;
590 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
591 if (err != 0 || query_value < 0) {
592 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
593 strerror(-err), err, query_value);
594 return VK_ERROR_SURFACE_LOST_KHR;
595 }
596
597 android_pixel_format native_format =
598 static_cast<android_pixel_format>(query_value);
599
600 bool format_supported = false;
601 switch (native_format) {
602 case HAL_PIXEL_FORMAT_RGBA_8888:
603 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700604 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800605 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700606 format_supported = true;
607 break;
608 default:
609 break;
610 }
611
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700612 *supported = static_cast<VkBool32>(
613 format_supported || (surface->consumer_usage &
614 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
615 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700616
Jesse Halla6429252015-11-29 18:59:42 -0800617 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800618}
619
Jesse Halle1b12782015-11-30 11:27:32 -0800620VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800621VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800622 VkPhysicalDevice /*pdev*/,
623 VkSurfaceKHR surface,
624 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800625 ATRACE_CALL();
626
Jesse Halld7b994a2015-09-07 14:17:37 -0700627 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800628 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700629
630 int width, height;
631 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
632 if (err != 0) {
633 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
634 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700635 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700636 }
637 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
638 if (err != 0) {
639 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
640 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700641 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700642 }
643
Jesse Hall55bc0972016-02-23 16:43:29 -0800644 int transform_hint;
645 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
646 if (err != 0) {
647 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
648 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700649 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800650 }
651
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800652 int max_buffer_count;
653 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
654 if (err != 0) {
655 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
656 strerror(-err), err);
657 return VK_ERROR_SURFACE_LOST_KHR;
658 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700659 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800660 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700661
Jesse Hallfe2662d2016-02-09 13:26:59 -0800662 capabilities->currentExtent =
663 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
664
Yiwei Zhang70a21962019-05-31 17:26:52 -0700665 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800666 capabilities->minImageExtent = VkExtent2D{1, 1};
667 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700668
Jesse Hallfe2662d2016-02-09 13:26:59 -0800669 capabilities->maxImageArrayLayers = 1;
670
Jesse Hall55bc0972016-02-23 16:43:29 -0800671 capabilities->supportedTransforms = kSupportedTransforms;
672 capabilities->currentTransform =
673 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700674
Jesse Hallfe2662d2016-02-09 13:26:59 -0800675 // On Android, window composition is a WindowManager property, not something
676 // associated with the bufferqueue. It can't be changed from here.
677 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700678
Jesse Hallb00daad2015-11-29 19:46:20 -0800679 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800680 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
681 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
682 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700683 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
684
Jesse Hallb1352bc2015-09-04 16:12:33 -0700685 return VK_SUCCESS;
686}
687
Jesse Halle1b12782015-11-30 11:27:32 -0800688VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700689VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
690 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800691 uint32_t* count,
692 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800693 ATRACE_CALL();
694
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700695 const InstanceData& instance_data = GetData(pdev);
696
Jesse Hall1356b0d2015-11-23 17:24:58 -0800697 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
698 // a new gralloc method to query whether a (format, usage) pair is
699 // supported, and check that for each gralloc format that corresponds to a
700 // Vulkan format. Shorter term, just add a few more formats to the ones
701 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700702
703 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700704 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
705 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
706 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Ian Elliott94ace212019-02-20 14:05:29 -0700707 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
708 {VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700709 };
710 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700711 uint32_t total_num_formats = kNumFormats;
712
713 bool wide_color_support = false;
714 Surface& surface = *SurfaceFromHandle(surface_handle);
715 int err = native_window_get_wide_color_support(surface.window.get(),
716 &wide_color_support);
717 if (err) {
Yiwei Zhang70a21962019-05-31 17:26:52 -0700718 return VK_ERROR_SURFACE_LOST_KHR;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700719 }
720 ALOGV("wide_color_support is: %d", wide_color_support);
721 wide_color_support =
722 wide_color_support &&
723 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
724
725 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600726 {VK_FORMAT_R8G8B8A8_UNORM,
727 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
728 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700729 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700730 {VK_FORMAT_R16G16B16A16_SFLOAT,
731 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
732 {VK_FORMAT_R16G16B16A16_SFLOAT,
733 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800734 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
735 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700736 };
737 const uint32_t kNumWideColorFormats =
738 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
739 if (wide_color_support) {
740 total_num_formats += kNumWideColorFormats;
741 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700742
743 VkResult result = VK_SUCCESS;
744 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700745 uint32_t out_count = 0;
746 uint32_t transfer_count = 0;
747 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700748 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700749 transfer_count = std::min(*count, kNumFormats);
750 std::copy(kFormats, kFormats + transfer_count, formats);
751 out_count += transfer_count;
752 if (wide_color_support) {
753 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
754 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
755 formats + out_count);
756 out_count += transfer_count;
757 }
758 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700759 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700760 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700761 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700762 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700763}
764
Jesse Halle1b12782015-11-30 11:27:32 -0800765VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300766VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
767 VkPhysicalDevice physicalDevice,
768 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
769 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800770 ATRACE_CALL();
771
Chris Forbes2452cf72017-03-16 16:30:17 +1300772 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
773 physicalDevice, pSurfaceInfo->surface,
774 &pSurfaceCapabilities->surfaceCapabilities);
775
Chris Forbes06bc0092017-03-16 16:46:05 +1300776 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
777 while (caps->pNext) {
778 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
779
780 switch (caps->sType) {
781 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
782 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
783 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
784 caps);
785 // Claim same set of usage flags are supported for
786 // shared present modes as for other modes.
787 shared_caps->sharedPresentSupportedUsageFlags =
788 pSurfaceCapabilities->surfaceCapabilities
789 .supportedUsageFlags;
790 } break;
791
792 default:
793 // Ignore all other extension structs
794 break;
795 }
796 }
797
Chris Forbes2452cf72017-03-16 16:30:17 +1300798 return result;
799}
800
801VKAPI_ATTR
802VkResult GetPhysicalDeviceSurfaceFormats2KHR(
803 VkPhysicalDevice physicalDevice,
804 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
805 uint32_t* pSurfaceFormatCount,
806 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800807 ATRACE_CALL();
808
Chris Forbes2452cf72017-03-16 16:30:17 +1300809 if (!pSurfaceFormats) {
810 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
811 pSurfaceInfo->surface,
812 pSurfaceFormatCount, nullptr);
813 } else {
814 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
815 // after the call.
816 android::Vector<VkSurfaceFormatKHR> surface_formats;
817 surface_formats.resize(*pSurfaceFormatCount);
818 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
819 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
820 &surface_formats.editItemAt(0));
821
822 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
823 // marshal results individually due to stride difference.
824 // completely ignore any chained extension structs.
825 uint32_t formats_to_marshal = *pSurfaceFormatCount;
826 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
827 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
828 }
829 }
830
831 return result;
832 }
833}
834
835VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300836VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800837 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800838 uint32_t* count,
839 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800840 ATRACE_CALL();
841
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800842 int err;
843 int query_value;
844 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
845
846 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
847 if (err != 0 || query_value < 0) {
848 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
849 strerror(-err), err, query_value);
850 return VK_ERROR_SURFACE_LOST_KHR;
851 }
852 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
853
854 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
855 if (err != 0 || query_value < 0) {
856 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
857 strerror(-err), err, query_value);
858 return VK_ERROR_SURFACE_LOST_KHR;
859 }
860 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
861
Chris Forbese8d79a62017-02-22 12:49:18 +1300862 android::Vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800863 if (min_undequeued_buffers + 1 < max_buffer_count)
864 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300865 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
866
867 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
868 if (QueryPresentationProperties(pdev, &present_properties)) {
869 if (present_properties.sharedImage) {
870 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
871 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
872 }
873 }
874
875 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700876
877 VkResult result = VK_SUCCESS;
878 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300879 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700880 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300881 *count = std::min(*count, num_modes);
882 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700883 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300884 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700885 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700886 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700887}
888
Jesse Halle1b12782015-11-30 11:27:32 -0800889VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400890VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600891 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400892 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800893 ATRACE_CALL();
894
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400895 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
896 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
897 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
898 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
899 pDeviceGroupPresentCapabilities->sType);
900
901 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
902 sizeof(pDeviceGroupPresentCapabilities->presentMask));
903
904 // assume device group of size 1
905 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
906 pDeviceGroupPresentCapabilities->modes =
907 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
908
909 return VK_SUCCESS;
910}
911
912VKAPI_ATTR
913VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600914 VkDevice,
915 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400916 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800917 ATRACE_CALL();
918
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400919 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
920 return VK_SUCCESS;
921}
922
923VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600924VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400925 VkSurfaceKHR surface,
926 uint32_t* pRectCount,
927 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800928 ATRACE_CALL();
929
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400930 if (!pRects) {
931 *pRectCount = 1;
932 } else {
933 uint32_t count = std::min(*pRectCount, 1u);
934 bool incomplete = *pRectCount < 1;
935
936 *pRectCount = count;
937
938 if (incomplete) {
939 return VK_INCOMPLETE;
940 }
941
942 int err;
943 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
944
945 int width = 0, height = 0;
946 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
947 if (err != 0) {
948 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
949 strerror(-err), err);
950 }
951 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
952 if (err != 0) {
953 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
954 strerror(-err), err);
955 }
956
957 // TODO: Return something better than "whole window"
958 pRects[0].offset.x = 0;
959 pRects[0].offset.y = 0;
960 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
961 static_cast<uint32_t>(height)};
962 }
963 return VK_SUCCESS;
964}
965
966VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800967VkResult CreateSwapchainKHR(VkDevice device,
968 const VkSwapchainCreateInfoKHR* create_info,
969 const VkAllocationCallbacks* allocator,
970 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800971 ATRACE_CALL();
972
Jesse Halld7b994a2015-09-07 14:17:37 -0700973 int err;
974 VkResult result = VK_SUCCESS;
975
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700976 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
977 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
978 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
979 " oldSwapchain=0x%" PRIx64,
980 reinterpret_cast<uint64_t>(create_info->surface),
981 create_info->minImageCount, create_info->imageFormat,
982 create_info->imageColorSpace, create_info->imageExtent.width,
983 create_info->imageExtent.height, create_info->imageUsage,
984 create_info->preTransform, create_info->presentMode,
985 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
986
Jesse Hall1f91d392015-12-11 16:28:44 -0800987 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800988 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800989
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700990 android_pixel_format native_pixel_format =
991 GetNativePixelFormat(create_info->imageFormat);
992 android_dataspace native_dataspace =
993 GetNativeDataspace(create_info->imageColorSpace);
994 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
995 ALOGE(
996 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
997 "failed: Unsupported color space",
998 create_info->imageColorSpace);
999 return VK_ERROR_INITIALIZATION_FAILED;
1000 }
1001
Jesse Hall42a9eec2016-06-03 12:39:49 -07001002 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001003 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001004 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001005 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001006 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001007 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001008 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001009 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001010 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1011 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001012 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001013 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001014
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001015 Surface& surface = *SurfaceFromHandle(create_info->surface);
1016
Jesse Halldc225072016-05-30 22:40:14 -07001017 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001018 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001019 " because it already has active swapchain 0x%" PRIx64
1020 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1021 reinterpret_cast<uint64_t>(create_info->surface),
1022 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1023 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1024 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1025 }
1026 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1027 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1028
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001029 // -- Reset the native window --
1030 // The native window might have been used previously, and had its properties
1031 // changed from defaults. That will affect the answer we get for queries
1032 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1033 // attempt such queries.
1034
Jesse Halldc225072016-05-30 22:40:14 -07001035 // The native window only allows dequeueing all buffers before any have
1036 // been queued, since after that point at least one is assumed to be in
1037 // non-FREE state at any given time. Disconnecting and re-connecting
1038 // orphans the previous buffers, getting us back to the state where we can
1039 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001040 //
1041 // TODO(http://b/134186185) recycle swapchain images more efficiently
Jesse Halldc225072016-05-30 22:40:14 -07001042 err = native_window_api_disconnect(surface.window.get(),
1043 NATIVE_WINDOW_API_EGL);
1044 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1045 strerror(-err), err);
1046 err =
1047 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1048 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1049 strerror(-err), err);
1050
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001051 err = native_window_set_buffer_count(surface.window.get(), 0);
1052 if (err != 0) {
1053 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1054 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001055 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001056 }
1057
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301058 int swap_interval =
1059 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1060 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001061 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001062 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1063 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001064 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001065 }
1066
Chris Forbesb8042d22017-01-18 18:07:05 +13001067 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1068 if (err != 0) {
1069 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1070 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001071 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001072 }
1073
1074 err = native_window_set_auto_refresh(surface.window.get(), false);
1075 if (err != 0) {
1076 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1077 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001078 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001079 }
1080
Jesse Halld7b994a2015-09-07 14:17:37 -07001081 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001082
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001083 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001084
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001085 err = native_window_set_buffers_format(surface.window.get(),
1086 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001087 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001088 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001089 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001090 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001091 }
1092 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001093 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001094 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001095 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001096 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001097 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001098 }
1099
Jesse Hall3dd678a2016-01-08 21:52:01 -08001100 err = native_window_set_buffers_dimensions(
1101 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1102 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001103 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001104 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1105 create_info->imageExtent.width, create_info->imageExtent.height,
1106 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001107 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001108 }
1109
Jesse Hall178b6962016-02-24 15:39:50 -08001110 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1111 // applied during rendering. native_window_set_transform() expects the
1112 // inverse: the transform the app is requesting that the compositor perform
1113 // during composition. With native windows, pre-transform works by rendering
1114 // with the same transform the compositor is applying (as in Vulkan), but
1115 // then requesting the inverse transform, so that when the compositor does
1116 // it's job the two transforms cancel each other out and the compositor ends
1117 // up applying an identity transform to the app's buffer.
1118 err = native_window_set_buffers_transform(
1119 surface.window.get(),
1120 InvertTransformToNative(create_info->preTransform));
1121 if (err != 0) {
Jesse Hall178b6962016-02-24 15:39:50 -08001122 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1123 InvertTransformToNative(create_info->preTransform),
1124 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001125 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001126 }
1127
Jesse Hallf64ca122015-11-03 16:11:10 -08001128 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001129 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001130 if (err != 0) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001131 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1132 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001133 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001134 }
1135
Chris Forbes97ef4612017-03-30 19:37:50 +13001136 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1137 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1138 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1139 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1140 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1141 if (err != 0) {
1142 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1143 return VK_ERROR_SURFACE_LOST_KHR;
1144 }
1145 }
1146
1147 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1148 err = native_window_set_auto_refresh(surface.window.get(), true);
1149 if (err != 0) {
1150 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1151 return VK_ERROR_SURFACE_LOST_KHR;
1152 }
1153 }
1154
Jesse Halle6080bf2016-02-28 20:58:50 -08001155 int query_value;
1156 err = surface.window->query(surface.window.get(),
1157 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1158 &query_value);
1159 if (err != 0 || query_value < 0) {
Jesse Halle6080bf2016-02-28 20:58:50 -08001160 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1161 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001162 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001163 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001164 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001165 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001166 (swap_interval ? create_info->minImageCount
1167 : std::max(3u, create_info->minImageCount)) -
1168 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001169
1170 // Lower layer insists that we have at least two buffers. This is wasteful
1171 // and we'd like to relax it in the shared case, but not all the pieces are
1172 // in place for that to work yet. Note we only lie to the lower layer-- we
1173 // don't want to give the app back a swapchain with extra images (which they
1174 // can't actually use!).
1175 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001176 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001177 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1178 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001179 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001180 }
1181
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001182 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001183 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001184 uint64_t consumer_usage, producer_usage;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001185 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001186 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1187 device, create_info->imageFormat, create_info->imageUsage,
1188 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001189 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001190 if (result != VK_SUCCESS) {
1191 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001192 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001193 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001194 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001195 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001196 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001197 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001198 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001199 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001200 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001201 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001202 if (result != VK_SUCCESS) {
1203 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001204 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001205 }
Jesse Hall70f93352015-11-04 09:41:31 -08001206 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001207 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1208
1209 bool createProtectedSwapchain = false;
1210 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1211 createProtectedSwapchain = true;
1212 native_usage |= BufferUsage::PROTECTED;
1213 }
1214 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001215 if (err != 0) {
Jesse Hall70f93352015-11-04 09:41:31 -08001216 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001217 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001218 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001219
1220 // -- Allocate our Swapchain object --
1221 // After this point, we must deallocate the swapchain on error.
1222
Jesse Hall1f91d392015-12-11 16:28:44 -08001223 void* mem = allocator->pfnAllocation(allocator->pUserData,
1224 sizeof(Swapchain), alignof(Swapchain),
1225 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001226 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001227 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001228 Swapchain* swapchain = new (mem)
1229 Swapchain(surface, num_images, create_info->presentMode,
1230 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001231 // -- Dequeue all buffers and create a VkImage for each --
1232 // Any failures during or after this must cancel the dequeued buffers.
1233
Chris Forbesb56287a2017-01-12 14:28:58 +13001234 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1235#pragma clang diagnostic push
1236#pragma clang diagnostic ignored "-Wold-style-cast"
1237 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1238#pragma clang diagnostic pop
1239 .pNext = nullptr,
1240 .usage = swapchain_image_usage,
1241 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001242 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001243#pragma clang diagnostic push
1244#pragma clang diagnostic ignored "-Wold-style-cast"
1245 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1246#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001247 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001248 };
1249 VkImageCreateInfo image_create = {
1250 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1251 .pNext = &image_native_buffer,
1252 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001253 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001254 .extent = {0, 0, 1},
1255 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001256 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001257 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001258 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001259 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001260 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001261 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001262 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001263 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1264 };
1265
Jesse Halld7b994a2015-09-07 14:17:37 -07001266 for (uint32_t i = 0; i < num_images; i++) {
1267 Swapchain::Image& img = swapchain->images[i];
1268
1269 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001270 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1271 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001272 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001273 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001274 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001275 break;
1276 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001277 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001278 img.dequeued = true;
1279
1280 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001281 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1282 static_cast<uint32_t>(img.buffer->height),
1283 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001284 image_native_buffer.handle = img.buffer->handle;
1285 image_native_buffer.stride = img.buffer->stride;
1286 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001287 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001288 android_convertGralloc0To1Usage(int(img.buffer->usage),
1289 &image_native_buffer.usage2.producer,
1290 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001291
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001292 ATRACE_BEGIN("dispatch.CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001293 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001294 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001295 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001296 if (result != VK_SUCCESS) {
1297 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1298 break;
1299 }
1300 }
1301
1302 // -- Cancel all buffers, returning them to the queue --
1303 // If an error occurred before, also destroy the VkImage and release the
1304 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1305 //
1306 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1307 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001308 for (uint32_t i = 0; i < num_images; i++) {
1309 Swapchain::Image& img = swapchain->images[i];
1310 if (img.dequeued) {
1311 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001312 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1313 img.dequeue_fence);
1314 img.dequeue_fence = -1;
1315 img.dequeued = false;
1316 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001317 }
1318 if (result != VK_SUCCESS) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001319 if (img.image) {
1320 ATRACE_BEGIN("dispatch.DestroyImage");
Chris Forbes31b85c22018-05-29 15:03:28 -07001321 dispatch.DestroyImage(device, img.image, nullptr);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001322 ATRACE_END();
1323 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001324 }
1325 }
1326
1327 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001328 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001329 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001330 return result;
1331 }
1332
Jesse Halldc225072016-05-30 22:40:14 -07001333 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1334 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001335 return VK_SUCCESS;
1336}
1337
Jesse Halle1b12782015-11-30 11:27:32 -08001338VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001339void DestroySwapchainKHR(VkDevice device,
1340 VkSwapchainKHR swapchain_handle,
1341 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001342 ATRACE_CALL();
1343
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001344 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001345 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001346 if (!swapchain)
1347 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001348 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1349 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001350
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001351 if (swapchain->frame_timestamps_enabled) {
1352 native_window_enable_frame_timestamps(window, false);
1353 }
Jesse Halldc225072016-05-30 22:40:14 -07001354 for (uint32_t i = 0; i < swapchain->num_images; i++)
1355 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001356 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001357 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001358 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001359 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001360 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001361 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001362}
1363
Jesse Halle1b12782015-11-30 11:27:32 -08001364VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001365VkResult GetSwapchainImagesKHR(VkDevice,
1366 VkSwapchainKHR swapchain_handle,
1367 uint32_t* count,
1368 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001369 ATRACE_CALL();
1370
Jesse Halld7b994a2015-09-07 14:17:37 -07001371 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001372 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1373 "getting images for non-active swapchain 0x%" PRIx64
1374 "; only dequeued image handles are valid",
1375 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001376 VkResult result = VK_SUCCESS;
1377 if (images) {
1378 uint32_t n = swapchain.num_images;
1379 if (*count < swapchain.num_images) {
1380 n = *count;
1381 result = VK_INCOMPLETE;
1382 }
1383 for (uint32_t i = 0; i < n; i++)
1384 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001385 *count = n;
1386 } else {
1387 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001388 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001389 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001390}
1391
Jesse Halle1b12782015-11-30 11:27:32 -08001392VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001393VkResult AcquireNextImageKHR(VkDevice device,
1394 VkSwapchainKHR swapchain_handle,
1395 uint64_t timeout,
1396 VkSemaphore semaphore,
1397 VkFence vk_fence,
1398 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001399 ATRACE_CALL();
1400
Jesse Halld7b994a2015-09-07 14:17:37 -07001401 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001402 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001403 VkResult result;
1404 int err;
1405
Jesse Halldc225072016-05-30 22:40:14 -07001406 if (swapchain.surface.swapchain_handle != swapchain_handle)
1407 return VK_ERROR_OUT_OF_DATE_KHR;
1408
Jesse Halld7b994a2015-09-07 14:17:37 -07001409 ALOGW_IF(
1410 timeout != UINT64_MAX,
1411 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1412
Chris Forbesc88409c2017-03-30 19:47:37 +13001413 if (swapchain.shared) {
1414 // In shared mode, we keep the buffer dequeued all the time, so we don't
1415 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1416 // the semaphore and fence passed to us will be signalled.
1417 *image_index = 0;
1418 result = GetData(device).driver.AcquireImageANDROID(
1419 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1420 return result;
1421 }
1422
Jesse Halld7b994a2015-09-07 14:17:37 -07001423 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001424 int fence_fd;
1425 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001426 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001427 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001428 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001429 }
1430
1431 uint32_t idx;
1432 for (idx = 0; idx < swapchain.num_images; idx++) {
1433 if (swapchain.images[idx].buffer.get() == buffer) {
1434 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001435 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001436 break;
1437 }
1438 }
1439 if (idx == swapchain.num_images) {
1440 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001441 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001442 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001443 }
1444
1445 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001446 if (fence_fd != -1) {
1447 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001448 if (fence_clone == -1) {
1449 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1450 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001451 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001452 }
1453 }
1454
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001455 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001456 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001457 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001458 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1459 // even if the call fails. We could close it ourselves on failure, but
1460 // that would create a race condition if the driver closes it on a
1461 // failure path: some other thread might create an fd with the same
1462 // number between the time the driver closes it and the time we close
1463 // it. We must assume one of: the driver *always* closes it even on
1464 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001465 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001466 swapchain.images[idx].dequeued = false;
1467 swapchain.images[idx].dequeue_fence = -1;
1468 return result;
1469 }
1470
1471 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001472 return VK_SUCCESS;
1473}
1474
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001475VKAPI_ATTR
1476VkResult AcquireNextImage2KHR(VkDevice device,
1477 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1478 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001479 ATRACE_CALL();
1480
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001481 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1482 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1483 pAcquireInfo->fence, pImageIndex);
1484}
1485
Jesse Halldc225072016-05-30 22:40:14 -07001486static VkResult WorstPresentResult(VkResult a, VkResult b) {
1487 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1488 // (in spec version 1.0.14).
1489 static const VkResult kWorstToBest[] = {
1490 VK_ERROR_DEVICE_LOST,
1491 VK_ERROR_SURFACE_LOST_KHR,
1492 VK_ERROR_OUT_OF_DATE_KHR,
1493 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1494 VK_ERROR_OUT_OF_HOST_MEMORY,
1495 VK_SUBOPTIMAL_KHR,
1496 };
1497 for (auto result : kWorstToBest) {
1498 if (a == result || b == result)
1499 return result;
1500 }
1501 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1502 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1503 return a != VK_SUCCESS ? a : b;
1504}
1505
Jesse Halle1b12782015-11-30 11:27:32 -08001506VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001507VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001508 ATRACE_CALL();
1509
Jesse Halld7b994a2015-09-07 14:17:37 -07001510 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1511 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1512 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001513
Jesse Halldc225072016-05-30 22:40:14 -07001514 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001515 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001516 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001517
Ian Elliottcb351132016-12-13 10:30:40 -07001518 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001519 const VkPresentRegionsKHR* present_regions = nullptr;
1520 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001521 const VkPresentRegionsKHR* next =
1522 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1523 while (next) {
1524 switch (next->sType) {
1525 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1526 present_regions = next;
1527 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001528 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001529 present_times =
1530 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1531 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001532 default:
1533 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1534 next->sType);
1535 break;
1536 }
1537 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1538 }
1539 ALOGV_IF(
1540 present_regions &&
1541 present_regions->swapchainCount != present_info->swapchainCount,
1542 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001543 ALOGV_IF(present_times &&
1544 present_times->swapchainCount != present_info->swapchainCount,
1545 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1546 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001547 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001548 (present_regions) ? present_regions->pRegions : nullptr;
1549 const VkPresentTimeGOOGLE* times =
1550 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001551 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001552 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001553 uint32_t nrects = 0;
1554
Jesse Halld7b994a2015-09-07 14:17:37 -07001555 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1556 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001557 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001558 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001559 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001560 const VkPresentRegionKHR* region =
1561 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001562 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001563 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001564 VkResult result;
1565 int err;
1566
Jesse Halld7b994a2015-09-07 14:17:37 -07001567 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001568 result = dispatch.QueueSignalReleaseImageANDROID(
1569 queue, present_info->waitSemaphoreCount,
1570 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001571 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001572 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001573 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001574 }
1575
Jesse Halldc225072016-05-30 22:40:14 -07001576 if (swapchain.surface.swapchain_handle ==
1577 present_info->pSwapchains[sc]) {
1578 ANativeWindow* window = swapchain.surface.window.get();
1579 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001580 if (region) {
1581 // Process the incremental-present hint for this swapchain:
1582 uint32_t rcount = region->rectangleCount;
1583 if (rcount > nrects) {
1584 android_native_rect_t* new_rects =
1585 static_cast<android_native_rect_t*>(
1586 allocator->pfnReallocation(
1587 allocator->pUserData, rects,
1588 sizeof(android_native_rect_t) * rcount,
1589 alignof(android_native_rect_t),
1590 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1591 if (new_rects) {
1592 rects = new_rects;
1593 nrects = rcount;
1594 } else {
1595 rcount = 0; // Ignore the hint for this swapchain
1596 }
1597 }
1598 for (uint32_t r = 0; r < rcount; ++r) {
1599 if (region->pRectangles[r].layer > 0) {
1600 ALOGV(
1601 "vkQueuePresentKHR ignoring invalid layer "
1602 "(%u); using layer 0 instead",
1603 region->pRectangles[r].layer);
1604 }
1605 int x = region->pRectangles[r].offset.x;
1606 int y = region->pRectangles[r].offset.y;
1607 int width = static_cast<int>(
1608 region->pRectangles[r].extent.width);
1609 int height = static_cast<int>(
1610 region->pRectangles[r].extent.height);
1611 android_native_rect_t* cur_rect = &rects[r];
1612 cur_rect->left = x;
1613 cur_rect->top = y + height;
1614 cur_rect->right = x + width;
1615 cur_rect->bottom = y;
1616 }
1617 native_window_set_surface_damage(window, rects, rcount);
1618 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001619 if (time) {
1620 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001621 ALOGV(
1622 "Calling "
1623 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001624 native_window_enable_frame_timestamps(window, true);
1625 swapchain.frame_timestamps_enabled = true;
1626 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001627
1628 // Record the nativeFrameId so it can be later correlated to
1629 // this present.
1630 uint64_t nativeFrameId = 0;
1631 err = native_window_get_next_frame_id(
1632 window, &nativeFrameId);
1633 if (err != android::NO_ERROR) {
1634 ALOGE("Failed to get next native frame ID.");
1635 }
1636
1637 // Add a new timing record with the user's presentID and
1638 // the nativeFrameId.
1639 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1640 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001641 swapchain.timing.removeAt(0);
1642 }
1643 if (time->desiredPresentTime) {
1644 // Set the desiredPresentTime:
1645 ALOGV(
1646 "Calling "
1647 "native_window_set_buffers_timestamp(%" PRId64 ")",
1648 time->desiredPresentTime);
1649 native_window_set_buffers_timestamp(
1650 window,
1651 static_cast<int64_t>(time->desiredPresentTime));
1652 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001653 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001654
Jesse Halldc225072016-05-30 22:40:14 -07001655 err = window->queueBuffer(window, img.buffer.get(), fence);
1656 // queueBuffer always closes fence, even on error
1657 if (err != 0) {
1658 // TODO(jessehall): What now? We should probably cancel the
1659 // buffer, I guess?
1660 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1661 swapchain_result = WorstPresentResult(
1662 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1663 }
1664 if (img.dequeue_fence >= 0) {
1665 close(img.dequeue_fence);
1666 img.dequeue_fence = -1;
1667 }
1668 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001669
1670 // If the swapchain is in shared mode, immediately dequeue the
1671 // buffer so it can be presented again without an intervening
1672 // call to AcquireNextImageKHR. We expect to get the same buffer
1673 // back from every call to dequeueBuffer in this mode.
1674 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1675 ANativeWindowBuffer* buffer;
1676 int fence_fd;
1677 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1678 if (err != 0) {
1679 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1680 swapchain_result = WorstPresentResult(swapchain_result,
1681 VK_ERROR_SURFACE_LOST_KHR);
1682 }
1683 else if (img.buffer != buffer) {
1684 ALOGE("got wrong image back for shared swapchain");
1685 swapchain_result = WorstPresentResult(swapchain_result,
1686 VK_ERROR_SURFACE_LOST_KHR);
1687 }
1688 else {
1689 img.dequeue_fence = fence_fd;
1690 img.dequeued = true;
1691 }
1692 }
Jesse Halldc225072016-05-30 22:40:14 -07001693 }
1694 if (swapchain_result != VK_SUCCESS) {
1695 ReleaseSwapchainImage(device, window, fence, img);
1696 OrphanSwapchain(device, &swapchain);
1697 }
silence_dogood73597592019-05-23 16:57:37 -07001698 int window_transform_hint;
1699 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1700 &window_transform_hint);
1701 if (err != 0) {
1702 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1703 strerror(-err), err);
1704 swapchain_result = WorstPresentResult(
1705 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1706 }
1707 if (swapchain.pre_transform != window_transform_hint) {
1708 swapchain_result =
1709 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1710 }
Jesse Halldc225072016-05-30 22:40:14 -07001711 } else {
1712 ReleaseSwapchainImage(device, nullptr, fence, img);
1713 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001714 }
1715
Jesse Halla9e57032015-11-30 01:03:10 -08001716 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001717 present_info->pResults[sc] = swapchain_result;
1718
1719 if (swapchain_result != final_result)
1720 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001721 }
Ian Elliottcb351132016-12-13 10:30:40 -07001722 if (rects) {
1723 allocator->pfnFree(allocator->pUserData, rects);
1724 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001725
1726 return final_result;
1727}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001728
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001729VKAPI_ATTR
1730VkResult GetRefreshCycleDurationGOOGLE(
1731 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001732 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001733 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001734 ATRACE_CALL();
1735
Ian Elliott62c48c92017-01-20 13:13:20 -07001736 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001737 VkResult result = VK_SUCCESS;
1738
Ian Elliott3568bfe2019-05-03 15:54:46 -06001739 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001740
1741 return result;
1742}
1743
1744VKAPI_ATTR
1745VkResult GetPastPresentationTimingGOOGLE(
1746 VkDevice,
1747 VkSwapchainKHR swapchain_handle,
1748 uint32_t* count,
1749 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001750 ATRACE_CALL();
1751
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001752 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1753 ANativeWindow* window = swapchain.surface.window.get();
1754 VkResult result = VK_SUCCESS;
1755
1756 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001757 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001758 native_window_enable_frame_timestamps(window, true);
1759 swapchain.frame_timestamps_enabled = true;
1760 }
1761
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001762 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001763 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1764 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001765 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001766 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001767 }
1768
1769 return result;
1770}
1771
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001772VKAPI_ATTR
1773VkResult GetSwapchainStatusKHR(
1774 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001775 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001776 ATRACE_CALL();
1777
Chris Forbes4e18ba82017-01-20 12:50:17 +13001778 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001779 VkResult result = VK_SUCCESS;
1780
Chris Forbes4e18ba82017-01-20 12:50:17 +13001781 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1782 return VK_ERROR_OUT_OF_DATE_KHR;
1783 }
1784
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001785 // TODO(chrisforbes): Implement this function properly
1786
1787 return result;
1788}
1789
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001790VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001791 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001792 uint32_t swapchainCount,
1793 const VkSwapchainKHR* pSwapchains,
1794 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001795 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001796
1797 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1798 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1799 if (!swapchain)
1800 continue;
1801
1802 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1803
1804 ANativeWindow* window = swapchain->surface.window.get();
1805
1806 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1807 const android_smpte2086_metadata smpteMetdata = {
1808 {vulkanMetadata.displayPrimaryRed.x,
1809 vulkanMetadata.displayPrimaryRed.y},
1810 {vulkanMetadata.displayPrimaryGreen.x,
1811 vulkanMetadata.displayPrimaryGreen.y},
1812 {vulkanMetadata.displayPrimaryBlue.x,
1813 vulkanMetadata.displayPrimaryBlue.y},
1814 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1815 vulkanMetadata.maxLuminance,
1816 vulkanMetadata.minLuminance};
1817 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1818
1819 const android_cta861_3_metadata cta8613Metadata = {
1820 vulkanMetadata.maxContentLightLevel,
1821 vulkanMetadata.maxFrameAverageLightLevel};
1822 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1823 }
1824
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001825 return;
1826}
1827
Yiwei Zhang0f475222019-04-11 19:38:00 -07001828static void InterceptBindImageMemory2(
1829 uint32_t bind_info_count,
1830 const VkBindImageMemoryInfo* bind_infos,
1831 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1832 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1833 out_native_buffers->clear();
1834 out_bind_infos->clear();
1835
1836 if (!bind_info_count)
1837 return;
1838
1839 std::unordered_set<uint32_t> intercepted_indexes;
1840
1841 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1842 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1843 bind_infos[idx].pNext);
1844 while (info &&
1845 info->sType !=
1846 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1847 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1848 info->pNext);
1849 }
1850
1851 if (!info)
1852 continue;
1853
1854 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1855 "swapchain handle must not be NULL");
1856 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1857 ALOG_ASSERT(
1858 info->imageIndex < swapchain->num_images,
1859 "imageIndex must be less than the number of images in swapchain");
1860
1861 ANativeWindowBuffer* buffer =
1862 swapchain->images[info->imageIndex].buffer.get();
1863 VkNativeBufferANDROID native_buffer = {
1864#pragma clang diagnostic push
1865#pragma clang diagnostic ignored "-Wold-style-cast"
1866 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1867#pragma clang diagnostic pop
1868 .pNext = bind_infos[idx].pNext,
1869 .handle = buffer->handle,
1870 .stride = buffer->stride,
1871 .format = buffer->format,
1872 .usage = int(buffer->usage),
1873 };
1874 // Reserve enough space to avoid letting re-allocation invalidate the
1875 // addresses of the elements inside.
1876 out_native_buffers->reserve(bind_info_count);
1877 out_native_buffers->emplace_back(native_buffer);
1878
1879 // Reserve the space now since we know how much is needed now.
1880 out_bind_infos->reserve(bind_info_count);
1881 out_bind_infos->emplace_back(bind_infos[idx]);
1882 out_bind_infos->back().pNext = &out_native_buffers->back();
1883
1884 intercepted_indexes.insert(idx);
1885 }
1886
1887 if (intercepted_indexes.empty())
1888 return;
1889
1890 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1891 if (intercepted_indexes.count(idx))
1892 continue;
1893 out_bind_infos->emplace_back(bind_infos[idx]);
1894 }
1895}
1896
Yiwei Zhang23143102019-04-10 18:24:05 -07001897VKAPI_ATTR
1898VkResult BindImageMemory2(VkDevice device,
1899 uint32_t bindInfoCount,
1900 const VkBindImageMemoryInfo* pBindInfos) {
1901 ATRACE_CALL();
1902
Yiwei Zhang0f475222019-04-11 19:38:00 -07001903 // out_native_buffers is for maintaining the lifecycle of the constructed
1904 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1905 std::vector<VkNativeBufferANDROID> out_native_buffers;
1906 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1907 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1908 &out_bind_infos);
1909 return GetData(device).driver.BindImageMemory2(
1910 device, bindInfoCount,
1911 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001912}
1913
1914VKAPI_ATTR
1915VkResult BindImageMemory2KHR(VkDevice device,
1916 uint32_t bindInfoCount,
1917 const VkBindImageMemoryInfo* pBindInfos) {
1918 ATRACE_CALL();
1919
Yiwei Zhang0f475222019-04-11 19:38:00 -07001920 std::vector<VkNativeBufferANDROID> out_native_buffers;
1921 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1922 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1923 &out_bind_infos);
1924 return GetData(device).driver.BindImageMemory2KHR(
1925 device, bindInfoCount,
1926 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001927}
1928
Chia-I Wu62262232016-03-26 07:06:44 +08001929} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001930} // namespace vulkan