Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 17 | #include <algorithm> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 18 | |
Jesse Hall | 7992781 | 2017-03-23 11:03:23 -0700 | [diff] [blame] | 19 | #include <grallocusage/GrallocUsageConversion.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 20 | #include <log/log.h> |
Pawin Vongmasa | 6e1193a | 2017-03-07 13:08:40 -0800 | [diff] [blame] | 21 | #include <ui/BufferQueueDefs.h> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 22 | #include <sync/sync.h> |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 23 | #include <utils/StrongPointer.h> |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 24 | #include <utils/Vector.h> |
Mathias Agopian | 6a3c05b | 2017-04-27 20:06:55 -0700 | [diff] [blame] | 25 | #include <system/window.h> |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 26 | #include <android/hardware/graphics/common/1.0/types.h> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 27 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 28 | #include "driver.h" |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 29 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 30 | using android::hardware::graphics::common::V1_0::BufferUsage; |
| 31 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 32 | // TODO(jessehall): Currently we don't have a good error code for when a native |
| 33 | // window operation fails. Just returning INITIALIZATION_FAILED for now. Later |
| 34 | // versions (post SDK 0.9) of the API/extension have a better error code. |
| 35 | // When updating to that version, audit all error returns. |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 36 | namespace vulkan { |
| 37 | namespace driver { |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 38 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 39 | namespace { |
| 40 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 41 | const VkSurfaceTransformFlagsKHR kSupportedTransforms = |
| 42 | VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR | |
| 43 | VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR | |
| 44 | VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR | |
| 45 | VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR | |
| 46 | // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform. |
| 47 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR | |
| 48 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR | |
| 49 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR | |
| 50 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR | |
| 51 | VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR; |
| 52 | |
| 53 | VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) { |
| 54 | // Native and Vulkan transforms are isomorphic, but are represented |
| 55 | // differently. Vulkan transforms are built up of an optional horizontal |
| 56 | // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native |
| 57 | // transforms are built up from a horizontal flip, vertical flip, and |
| 58 | // 90-degree rotation, all optional but always in that order. |
| 59 | |
| 60 | // TODO(jessehall): For now, only support pure rotations, not |
| 61 | // flip or flip-and-rotate, until I have more time to test them and build |
| 62 | // sample code. As far as I know we never actually use anything besides |
| 63 | // pure rotations anyway. |
| 64 | |
| 65 | switch (native) { |
| 66 | case 0: // 0x0 |
| 67 | return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 68 | // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1 |
| 69 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR; |
| 70 | // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2 |
| 71 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR; |
| 72 | case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V |
| 73 | return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR; |
| 74 | case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4 |
| 75 | return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR; |
| 76 | // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90: |
| 77 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR; |
| 78 | // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90: |
| 79 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR; |
| 80 | case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90 |
| 81 | return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR; |
| 82 | case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY: |
| 83 | default: |
| 84 | return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 85 | } |
| 86 | } |
| 87 | |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 88 | int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) { |
| 89 | switch (transform) { |
| 90 | case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR: |
| 91 | return NATIVE_WINDOW_TRANSFORM_ROT_270; |
| 92 | case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR: |
| 93 | return NATIVE_WINDOW_TRANSFORM_ROT_180; |
| 94 | case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR: |
| 95 | return NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 96 | // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform. |
| 97 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR: |
| 98 | // return NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 99 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR: |
| 100 | // return NATIVE_WINDOW_TRANSFORM_FLIP_H | |
| 101 | // NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 102 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR: |
| 103 | // return NATIVE_WINDOW_TRANSFORM_FLIP_V; |
| 104 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR: |
| 105 | // return NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 106 | // NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 107 | case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR: |
| 108 | case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR: |
| 109 | default: |
| 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 114 | class TimingInfo { |
| 115 | public: |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 116 | TimingInfo() = default; |
| 117 | TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId) |
Ian Elliott | 2c6355d | 2017-01-19 11:02:13 -0700 | [diff] [blame] | 118 | : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0}, |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 119 | native_frame_id_(nativeFrameId) {} |
| 120 | bool ready() const { |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 121 | return (timestamp_desired_present_time_ != |
| 122 | NATIVE_WINDOW_TIMESTAMP_PENDING && |
| 123 | timestamp_actual_present_time_ != |
| 124 | NATIVE_WINDOW_TIMESTAMP_PENDING && |
| 125 | timestamp_render_complete_time_ != |
| 126 | NATIVE_WINDOW_TIMESTAMP_PENDING && |
| 127 | timestamp_composition_latch_time_ != |
| 128 | NATIVE_WINDOW_TIMESTAMP_PENDING); |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 129 | } |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 130 | void calculate(int64_t rdur) { |
| 131 | bool anyTimestampInvalid = |
| 132 | (timestamp_actual_present_time_ == |
| 133 | NATIVE_WINDOW_TIMESTAMP_INVALID) || |
| 134 | (timestamp_render_complete_time_ == |
| 135 | NATIVE_WINDOW_TIMESTAMP_INVALID) || |
| 136 | (timestamp_composition_latch_time_ == |
| 137 | NATIVE_WINDOW_TIMESTAMP_INVALID); |
| 138 | if (anyTimestampInvalid) { |
| 139 | ALOGE("Unexpectedly received invalid timestamp."); |
| 140 | vals_.actualPresentTime = 0; |
| 141 | vals_.earliestPresentTime = 0; |
| 142 | vals_.presentMargin = 0; |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | vals_.actualPresentTime = |
| 147 | static_cast<uint64_t>(timestamp_actual_present_time_); |
| 148 | int64_t margin = (timestamp_composition_latch_time_ - |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 149 | timestamp_render_complete_time_); |
| 150 | // Calculate vals_.earliestPresentTime, and potentially adjust |
| 151 | // vals_.presentMargin. The initial value of vals_.earliestPresentTime |
| 152 | // is vals_.actualPresentTime. If we can subtract rdur (the duration |
| 153 | // of a refresh cycle) from vals_.earliestPresentTime (and also from |
| 154 | // vals_.presentMargin) and still leave a positive margin, then we can |
| 155 | // report to the application that it could have presented earlier than |
| 156 | // it did (per the extension specification). If for some reason, we |
| 157 | // can do this subtraction repeatedly, we do, since |
| 158 | // vals_.earliestPresentTime really is supposed to be the "earliest". |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 159 | int64_t early_time = timestamp_actual_present_time_; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 160 | while ((margin > rdur) && |
| 161 | ((early_time - rdur) > timestamp_composition_latch_time_)) { |
| 162 | early_time -= rdur; |
| 163 | margin -= rdur; |
| 164 | } |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 165 | vals_.earliestPresentTime = static_cast<uint64_t>(early_time); |
| 166 | vals_.presentMargin = static_cast<uint64_t>(margin); |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 167 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 168 | void get_values(VkPastPresentationTimingGOOGLE* values) const { |
| 169 | *values = vals_; |
| 170 | } |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 171 | |
| 172 | public: |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 173 | VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 }; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 174 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 175 | uint64_t native_frame_id_ { 0 }; |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 176 | int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING }; |
| 177 | int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING }; |
| 178 | int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING }; |
| 179 | int64_t timestamp_composition_latch_time_ |
| 180 | { NATIVE_WINDOW_TIMESTAMP_PENDING }; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 183 | // ---------------------------------------------------------------------------- |
| 184 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 185 | struct Surface { |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 186 | android::sp<ANativeWindow> window; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 187 | VkSwapchainKHR swapchain_handle; |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 188 | uint64_t consumer_usage; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | VkSurfaceKHR HandleFromSurface(Surface* surface) { |
| 192 | return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface)); |
| 193 | } |
| 194 | |
| 195 | Surface* SurfaceFromHandle(VkSurfaceKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 196 | return reinterpret_cast<Surface*>(handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 199 | // Maximum number of TimingInfo structs to keep per swapchain: |
| 200 | enum { MAX_TIMING_INFOS = 10 }; |
| 201 | // Minimum number of frames to look for in the past (so we don't cause |
| 202 | // syncronous requests to Surface Flinger): |
| 203 | enum { MIN_NUM_FRAMES_AGO = 5 }; |
| 204 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 205 | struct Swapchain { |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 206 | Swapchain(Surface& surface_, |
| 207 | uint32_t num_images_, |
| 208 | VkPresentModeKHR present_mode) |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 209 | : surface(surface_), |
| 210 | num_images(num_images_), |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 211 | mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR), |
Chris Forbes | f883564 | 2017-03-30 19:31:40 +1300 | [diff] [blame] | 212 | frame_timestamps_enabled(false), |
| 213 | shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR || |
| 214 | present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) { |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 215 | ANativeWindow* window = surface.window.get(); |
Ian Elliott | be833a2 | 2017-01-25 13:09:20 -0700 | [diff] [blame] | 216 | native_window_get_refresh_cycle_duration( |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 217 | window, |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 218 | &refresh_duration); |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 219 | } |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 220 | |
| 221 | Surface& surface; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 222 | uint32_t num_images; |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 223 | bool mailbox_mode; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 224 | bool frame_timestamps_enabled; |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 225 | int64_t refresh_duration; |
Chris Forbes | f883564 | 2017-03-30 19:31:40 +1300 | [diff] [blame] | 226 | bool shared; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 227 | |
| 228 | struct Image { |
| 229 | Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {} |
| 230 | VkImage image; |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 231 | android::sp<ANativeWindowBuffer> buffer; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 232 | // The fence is only valid when the buffer is dequeued, and should be |
| 233 | // -1 any other time. When valid, we own the fd, and must ensure it is |
| 234 | // closed: either by closing it explicitly when queueing the buffer, |
| 235 | // or by passing ownership e.g. to ANativeWindow::cancelBuffer(). |
| 236 | int dequeue_fence; |
| 237 | bool dequeued; |
Pawin Vongmasa | 6e1193a | 2017-03-07 13:08:40 -0800 | [diff] [blame] | 238 | } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS]; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 239 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 240 | android::Vector<TimingInfo> timing; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) { |
| 244 | return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain)); |
| 245 | } |
| 246 | |
| 247 | Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 248 | return reinterpret_cast<Swapchain*>(handle); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 251 | void ReleaseSwapchainImage(VkDevice device, |
| 252 | ANativeWindow* window, |
| 253 | int release_fence, |
| 254 | Swapchain::Image& image) { |
| 255 | ALOG_ASSERT(release_fence == -1 || image.dequeued, |
| 256 | "ReleaseSwapchainImage: can't provide a release fence for " |
| 257 | "non-dequeued images"); |
| 258 | |
| 259 | if (image.dequeued) { |
| 260 | if (release_fence >= 0) { |
| 261 | // We get here from vkQueuePresentKHR. The application is |
| 262 | // responsible for creating an execution dependency chain from |
| 263 | // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR |
| 264 | // (release_fence), so we can drop the dequeue_fence here. |
| 265 | if (image.dequeue_fence >= 0) |
| 266 | close(image.dequeue_fence); |
| 267 | } else { |
| 268 | // We get here during swapchain destruction, or various serious |
| 269 | // error cases e.g. when we can't create the release_fence during |
| 270 | // vkQueuePresentKHR. In non-error cases, the dequeue_fence should |
| 271 | // have already signalled, since the swapchain images are supposed |
| 272 | // to be idle before the swapchain is destroyed. In error cases, |
| 273 | // there may be rendering in flight to the image, but since we |
| 274 | // weren't able to create a release_fence, waiting for the |
| 275 | // dequeue_fence is about the best we can do. |
| 276 | release_fence = image.dequeue_fence; |
| 277 | } |
| 278 | image.dequeue_fence = -1; |
| 279 | |
| 280 | if (window) { |
| 281 | window->cancelBuffer(window, image.buffer.get(), release_fence); |
| 282 | } else { |
| 283 | if (release_fence >= 0) { |
| 284 | sync_wait(release_fence, -1 /* forever */); |
| 285 | close(release_fence); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | image.dequeued = false; |
| 290 | } |
| 291 | |
| 292 | if (image.image) { |
| 293 | GetData(device).driver.DestroyImage(device, image.image, nullptr); |
| 294 | image.image = VK_NULL_HANDLE; |
| 295 | } |
| 296 | |
| 297 | image.buffer.clear(); |
| 298 | } |
| 299 | |
| 300 | void OrphanSwapchain(VkDevice device, Swapchain* swapchain) { |
| 301 | if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain)) |
| 302 | return; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 303 | for (uint32_t i = 0; i < swapchain->num_images; i++) { |
| 304 | if (!swapchain->images[i].dequeued) |
| 305 | ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]); |
| 306 | } |
| 307 | swapchain->surface.swapchain_handle = VK_NULL_HANDLE; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 308 | swapchain->timing.clear(); |
| 309 | } |
| 310 | |
| 311 | uint32_t get_num_ready_timings(Swapchain& swapchain) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 312 | if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) { |
| 313 | return 0; |
| 314 | } |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 315 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 316 | uint32_t num_ready = 0; |
| 317 | const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1; |
| 318 | for (uint32_t i = 0; i < num_timings; i++) { |
| 319 | TimingInfo& ti = swapchain.timing.editItemAt(i); |
| 320 | if (ti.ready()) { |
| 321 | // This TimingInfo is ready to be reported to the user. Add it |
| 322 | // to the num_ready. |
| 323 | num_ready++; |
| 324 | continue; |
| 325 | } |
| 326 | // This TimingInfo is not yet ready to be reported to the user, |
| 327 | // and so we should look for any available timestamps that |
| 328 | // might make it ready. |
| 329 | int64_t desired_present_time = 0; |
| 330 | int64_t render_complete_time = 0; |
| 331 | int64_t composition_latch_time = 0; |
| 332 | int64_t actual_present_time = 0; |
| 333 | // Obtain timestamps: |
| 334 | int ret = native_window_get_frame_timestamps( |
| 335 | swapchain.surface.window.get(), ti.native_frame_id_, |
| 336 | &desired_present_time, &render_complete_time, |
| 337 | &composition_latch_time, |
| 338 | NULL, //&first_composition_start_time, |
| 339 | NULL, //&last_composition_start_time, |
| 340 | NULL, //&composition_finish_time, |
| 341 | // TODO(ianelliott): Maybe ask if this one is |
| 342 | // supported, at startup time (since it may not be |
| 343 | // supported): |
| 344 | &actual_present_time, |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 345 | NULL, //&dequeue_ready_time, |
| 346 | NULL /*&reads_done_time*/); |
| 347 | |
| 348 | if (ret != android::NO_ERROR) { |
| 349 | continue; |
| 350 | } |
| 351 | |
| 352 | // Record the timestamp(s) we received, and then see if this TimingInfo |
| 353 | // is ready to be reported to the user: |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 354 | ti.timestamp_desired_present_time_ = desired_present_time; |
| 355 | ti.timestamp_actual_present_time_ = actual_present_time; |
| 356 | ti.timestamp_render_complete_time_ = render_complete_time; |
| 357 | ti.timestamp_composition_latch_time_ = composition_latch_time; |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 358 | |
| 359 | if (ti.ready()) { |
| 360 | // The TimingInfo has received enough timestamps, and should now |
| 361 | // use those timestamps to calculate the info that should be |
| 362 | // reported to the user: |
| 363 | ti.calculate(swapchain.refresh_duration); |
| 364 | num_ready++; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | return num_ready; |
| 368 | } |
| 369 | |
| 370 | // TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!! |
| 371 | void copy_ready_timings(Swapchain& swapchain, |
| 372 | uint32_t* count, |
| 373 | VkPastPresentationTimingGOOGLE* timings) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 374 | if (swapchain.timing.empty()) { |
| 375 | *count = 0; |
| 376 | return; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 377 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 378 | |
| 379 | size_t last_ready = swapchain.timing.size() - 1; |
| 380 | while (!swapchain.timing[last_ready].ready()) { |
| 381 | if (last_ready == 0) { |
| 382 | *count = 0; |
| 383 | return; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 384 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 385 | last_ready--; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 386 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 387 | |
| 388 | uint32_t num_copied = 0; |
| 389 | size_t num_to_remove = 0; |
| 390 | for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) { |
| 391 | const TimingInfo& ti = swapchain.timing[i]; |
| 392 | if (ti.ready()) { |
| 393 | ti.get_values(&timings[num_copied]); |
| 394 | num_copied++; |
| 395 | } |
| 396 | num_to_remove++; |
| 397 | } |
| 398 | |
| 399 | // Discard old frames that aren't ready if newer frames are ready. |
| 400 | // We don't expect to get the timing info for those old frames. |
| 401 | swapchain.timing.removeItemsAt(0, num_to_remove); |
| 402 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 403 | *count = num_copied; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 406 | android_pixel_format GetNativePixelFormat(VkFormat format) { |
| 407 | android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 408 | switch (format) { |
| 409 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 410 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 411 | native_format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 412 | break; |
| 413 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 414 | native_format = HAL_PIXEL_FORMAT_RGB_565; |
| 415 | break; |
| 416 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 417 | native_format = HAL_PIXEL_FORMAT_RGBA_FP16; |
| 418 | break; |
| 419 | case VK_FORMAT_A2R10G10B10_UNORM_PACK32: |
| 420 | native_format = HAL_PIXEL_FORMAT_RGBA_1010102; |
| 421 | break; |
| 422 | default: |
| 423 | ALOGV("unsupported swapchain format %d", format); |
| 424 | break; |
| 425 | } |
| 426 | return native_format; |
| 427 | } |
| 428 | |
| 429 | android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) { |
| 430 | switch (colorspace) { |
| 431 | case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR: |
| 432 | return HAL_DATASPACE_V0_SRGB; |
| 433 | case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT: |
| 434 | return HAL_DATASPACE_DISPLAY_P3; |
| 435 | case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT: |
| 436 | return HAL_DATASPACE_V0_SCRGB_LINEAR; |
Courtney Goeltzenleuchter | b52abee | 2017-08-07 17:13:04 -0600 | [diff] [blame] | 437 | case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT: |
| 438 | return HAL_DATASPACE_V0_SCRGB; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 439 | case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT: |
| 440 | return HAL_DATASPACE_DCI_P3_LINEAR; |
| 441 | case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT: |
| 442 | return HAL_DATASPACE_DCI_P3; |
| 443 | case VK_COLOR_SPACE_BT709_LINEAR_EXT: |
| 444 | return HAL_DATASPACE_V0_SRGB_LINEAR; |
| 445 | case VK_COLOR_SPACE_BT709_NONLINEAR_EXT: |
| 446 | return HAL_DATASPACE_V0_SRGB; |
Courtney Goeltzenleuchter | c45673f | 2017-03-13 15:58:15 -0600 | [diff] [blame] | 447 | case VK_COLOR_SPACE_BT2020_LINEAR_EXT: |
| 448 | return HAL_DATASPACE_BT2020_LINEAR; |
| 449 | case VK_COLOR_SPACE_HDR10_ST2084_EXT: |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 450 | return static_cast<android_dataspace>( |
| 451 | HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 | |
| 452 | HAL_DATASPACE_RANGE_FULL); |
Courtney Goeltzenleuchter | c45673f | 2017-03-13 15:58:15 -0600 | [diff] [blame] | 453 | case VK_COLOR_SPACE_DOLBYVISION_EXT: |
| 454 | return static_cast<android_dataspace>( |
| 455 | HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 | |
| 456 | HAL_DATASPACE_RANGE_FULL); |
| 457 | case VK_COLOR_SPACE_HDR10_HLG_EXT: |
| 458 | return static_cast<android_dataspace>( |
| 459 | HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG | |
| 460 | HAL_DATASPACE_RANGE_FULL); |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 461 | case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT: |
| 462 | return static_cast<android_dataspace>( |
| 463 | HAL_DATASPACE_STANDARD_ADOBE_RGB | |
| 464 | HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL); |
| 465 | case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT: |
| 466 | return HAL_DATASPACE_ADOBE_RGB; |
| 467 | |
| 468 | // Pass through is intended to allow app to provide data that is passed |
| 469 | // to the display system without modification. |
| 470 | case VK_COLOR_SPACE_PASS_THROUGH_EXT: |
| 471 | return HAL_DATASPACE_ARBITRARY; |
| 472 | |
| 473 | default: |
| 474 | // This indicates that we don't know about the |
| 475 | // dataspace specified and we should indicate that |
| 476 | // it's unsupported |
| 477 | return HAL_DATASPACE_UNKNOWN; |
| 478 | } |
| 479 | } |
| 480 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 481 | } // anonymous namespace |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 482 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 483 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 484 | VkResult CreateAndroidSurfaceKHR( |
Jesse Hall | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 485 | VkInstance instance, |
| 486 | const VkAndroidSurfaceCreateInfoKHR* pCreateInfo, |
| 487 | const VkAllocationCallbacks* allocator, |
| 488 | VkSurfaceKHR* out_surface) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 489 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 490 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 491 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface), |
| 492 | alignof(Surface), |
| 493 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 494 | if (!mem) |
| 495 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 496 | Surface* surface = new (mem) Surface; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 497 | |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 498 | surface->window = pCreateInfo->window; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 499 | surface->swapchain_handle = VK_NULL_HANDLE; |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 500 | int err = native_window_get_consumer_usage(surface->window.get(), |
| 501 | &surface->consumer_usage); |
| 502 | if (err != android::NO_ERROR) { |
| 503 | ALOGE("native_window_get_consumer_usage() failed: %s (%d)", |
| 504 | strerror(-err), err); |
| 505 | surface->~Surface(); |
| 506 | allocator->pfnFree(allocator->pUserData, surface); |
| 507 | return VK_ERROR_INITIALIZATION_FAILED; |
| 508 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 509 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 510 | // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN. |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 511 | err = |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 512 | native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
| 513 | if (err != 0) { |
| 514 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 515 | // errors and translate them to valid Vulkan result codes? |
| 516 | ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err), |
| 517 | err); |
| 518 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 519 | allocator->pfnFree(allocator->pUserData, surface); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 520 | return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 521 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 522 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 523 | *out_surface = HandleFromSurface(surface); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 524 | return VK_SUCCESS; |
| 525 | } |
| 526 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 527 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 528 | void DestroySurfaceKHR(VkInstance instance, |
| 529 | VkSurfaceKHR surface_handle, |
| 530 | const VkAllocationCallbacks* allocator) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 531 | Surface* surface = SurfaceFromHandle(surface_handle); |
| 532 | if (!surface) |
| 533 | return; |
| 534 | native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 535 | ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 536 | "destroyed VkSurfaceKHR 0x%" PRIx64 |
| 537 | " has active VkSwapchainKHR 0x%" PRIx64, |
| 538 | reinterpret_cast<uint64_t>(surface_handle), |
| 539 | reinterpret_cast<uint64_t>(surface->swapchain_handle)); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 540 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 541 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 542 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 543 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 546 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 547 | VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/, |
| 548 | uint32_t /*queue_family*/, |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 549 | VkSurfaceKHR surface_handle, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 550 | VkBool32* supported) { |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 551 | const Surface* surface = SurfaceFromHandle(surface_handle); |
| 552 | if (!surface) { |
| 553 | return VK_ERROR_SURFACE_LOST_KHR; |
| 554 | } |
| 555 | const ANativeWindow* window = surface->window.get(); |
| 556 | |
| 557 | int query_value; |
| 558 | int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value); |
| 559 | if (err != 0 || query_value < 0) { |
| 560 | ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d", |
| 561 | strerror(-err), err, query_value); |
| 562 | return VK_ERROR_SURFACE_LOST_KHR; |
| 563 | } |
| 564 | |
| 565 | android_pixel_format native_format = |
| 566 | static_cast<android_pixel_format>(query_value); |
| 567 | |
| 568 | bool format_supported = false; |
| 569 | switch (native_format) { |
| 570 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 571 | case HAL_PIXEL_FORMAT_RGB_565: |
| 572 | format_supported = true; |
| 573 | break; |
| 574 | default: |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | // USAGE_CPU_READ_MASK 0xFUL |
| 579 | // USAGE_CPU_WRITE_MASK (0xFUL << 4) |
| 580 | // The currently used bits are as below: |
| 581 | // USAGE_CPU_READ_RARELY = 2UL |
| 582 | // USAGE_CPU_READ_OFTEN = 3UL |
| 583 | // USAGE_CPU_WRITE_RARELY = (2UL << 4) |
| 584 | // USAGE_CPU_WRITE_OFTEN = (3UL << 4) |
| 585 | *supported = static_cast<VkBool32>(format_supported || |
| 586 | (surface->consumer_usage & 0xFFUL) == 0); |
| 587 | |
Jesse Hall | a642925 | 2015-11-29 18:59:42 -0800 | [diff] [blame] | 588 | return VK_SUCCESS; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 591 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 592 | VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR( |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 593 | VkPhysicalDevice /*pdev*/, |
| 594 | VkSurfaceKHR surface, |
| 595 | VkSurfaceCapabilitiesKHR* capabilities) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 596 | int err; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 597 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 598 | |
| 599 | int width, height; |
| 600 | err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width); |
| 601 | if (err != 0) { |
| 602 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 603 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 604 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 605 | } |
| 606 | err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height); |
| 607 | if (err != 0) { |
| 608 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 609 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 610 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 613 | int transform_hint; |
| 614 | err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint); |
| 615 | if (err != 0) { |
| 616 | ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)", |
| 617 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 618 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 621 | // TODO(jessehall): Figure out what the min/max values should be. |
Yiwei Zhang | dbd9615 | 2018-02-08 14:22:53 -0800 | [diff] [blame] | 622 | int max_buffer_count; |
| 623 | err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count); |
| 624 | if (err != 0) { |
| 625 | ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)", |
| 626 | strerror(-err), err); |
| 627 | return VK_ERROR_SURFACE_LOST_KHR; |
| 628 | } |
Yiwei Zhang | 8e951d5 | 2018-04-12 13:19:46 -0700 | [diff] [blame] | 629 | capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2; |
Yiwei Zhang | dbd9615 | 2018-02-08 14:22:53 -0800 | [diff] [blame] | 630 | capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 631 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 632 | capabilities->currentExtent = |
| 633 | VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)}; |
| 634 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 635 | // TODO(jessehall): Figure out what the max extent should be. Maximum |
| 636 | // texture dimension maybe? |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 637 | capabilities->minImageExtent = VkExtent2D{1, 1}; |
| 638 | capabilities->maxImageExtent = VkExtent2D{4096, 4096}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 639 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 640 | capabilities->maxImageArrayLayers = 1; |
| 641 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 642 | capabilities->supportedTransforms = kSupportedTransforms; |
| 643 | capabilities->currentTransform = |
| 644 | TranslateNativeToVulkanTransform(transform_hint); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 645 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 646 | // On Android, window composition is a WindowManager property, not something |
| 647 | // associated with the bufferqueue. It can't be changed from here. |
| 648 | capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 649 | |
| 650 | // TODO(jessehall): I think these are right, but haven't thought hard about |
| 651 | // it. Do we need to query the driver for support of any of these? |
| 652 | // Currently not included: |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 653 | // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not |
| 654 | // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 655 | capabilities->supportedUsageFlags = |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 656 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 657 | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | |
| 658 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 659 | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 660 | |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 661 | return VK_SUCCESS; |
| 662 | } |
| 663 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 664 | VKAPI_ATTR |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 665 | VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev, |
| 666 | VkSurfaceKHR surface_handle, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 667 | uint32_t* count, |
| 668 | VkSurfaceFormatKHR* formats) { |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 669 | const InstanceData& instance_data = GetData(pdev); |
| 670 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 671 | // TODO(jessehall): Fill out the set of supported formats. Longer term, add |
| 672 | // a new gralloc method to query whether a (format, usage) pair is |
| 673 | // supported, and check that for each gralloc format that corresponds to a |
| 674 | // Vulkan format. Shorter term, just add a few more formats to the ones |
| 675 | // hardcoded below. |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 676 | |
| 677 | const VkSurfaceFormatKHR kFormats[] = { |
Jesse Hall | 2676338 | 2016-05-20 07:13:52 -0700 | [diff] [blame] | 678 | {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
| 679 | {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
| 680 | {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 681 | }; |
| 682 | const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]); |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 683 | uint32_t total_num_formats = kNumFormats; |
| 684 | |
| 685 | bool wide_color_support = false; |
| 686 | Surface& surface = *SurfaceFromHandle(surface_handle); |
| 687 | int err = native_window_get_wide_color_support(surface.window.get(), |
| 688 | &wide_color_support); |
| 689 | if (err) { |
| 690 | // Not allowed to return a more sensible error code, so do this |
| 691 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 692 | } |
| 693 | ALOGV("wide_color_support is: %d", wide_color_support); |
| 694 | wide_color_support = |
| 695 | wide_color_support && |
| 696 | instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace); |
| 697 | |
| 698 | const VkSurfaceFormatKHR kWideColorFormats[] = { |
Courtney Goeltzenleuchter | bd7e03a | 2017-09-28 11:34:18 -0600 | [diff] [blame] | 699 | {VK_FORMAT_R8G8B8A8_UNORM, |
| 700 | VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}, |
| 701 | {VK_FORMAT_R8G8B8A8_SRGB, |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 702 | VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}, |
| 703 | }; |
| 704 | const uint32_t kNumWideColorFormats = |
| 705 | sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]); |
| 706 | if (wide_color_support) { |
| 707 | total_num_formats += kNumWideColorFormats; |
| 708 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 709 | |
| 710 | VkResult result = VK_SUCCESS; |
| 711 | if (formats) { |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 712 | uint32_t out_count = 0; |
| 713 | uint32_t transfer_count = 0; |
| 714 | if (*count < total_num_formats) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 715 | result = VK_INCOMPLETE; |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 716 | transfer_count = std::min(*count, kNumFormats); |
| 717 | std::copy(kFormats, kFormats + transfer_count, formats); |
| 718 | out_count += transfer_count; |
| 719 | if (wide_color_support) { |
| 720 | transfer_count = std::min(*count - out_count, kNumWideColorFormats); |
| 721 | std::copy(kWideColorFormats, kWideColorFormats + transfer_count, |
| 722 | formats + out_count); |
| 723 | out_count += transfer_count; |
| 724 | } |
| 725 | *count = out_count; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 726 | } else { |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 727 | *count = total_num_formats; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 728 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 729 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 732 | VKAPI_ATTR |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 733 | VkResult GetPhysicalDeviceSurfaceCapabilities2KHR( |
| 734 | VkPhysicalDevice physicalDevice, |
| 735 | const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo, |
| 736 | VkSurfaceCapabilities2KHR* pSurfaceCapabilities) { |
| 737 | VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR( |
| 738 | physicalDevice, pSurfaceInfo->surface, |
| 739 | &pSurfaceCapabilities->surfaceCapabilities); |
| 740 | |
Chris Forbes | 06bc009 | 2017-03-16 16:46:05 +1300 | [diff] [blame] | 741 | VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities; |
| 742 | while (caps->pNext) { |
| 743 | caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext); |
| 744 | |
| 745 | switch (caps->sType) { |
| 746 | case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: { |
| 747 | VkSharedPresentSurfaceCapabilitiesKHR* shared_caps = |
| 748 | reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>( |
| 749 | caps); |
| 750 | // Claim same set of usage flags are supported for |
| 751 | // shared present modes as for other modes. |
| 752 | shared_caps->sharedPresentSupportedUsageFlags = |
| 753 | pSurfaceCapabilities->surfaceCapabilities |
| 754 | .supportedUsageFlags; |
| 755 | } break; |
| 756 | |
| 757 | default: |
| 758 | // Ignore all other extension structs |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 763 | return result; |
| 764 | } |
| 765 | |
| 766 | VKAPI_ATTR |
| 767 | VkResult GetPhysicalDeviceSurfaceFormats2KHR( |
| 768 | VkPhysicalDevice physicalDevice, |
| 769 | const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo, |
| 770 | uint32_t* pSurfaceFormatCount, |
| 771 | VkSurfaceFormat2KHR* pSurfaceFormats) { |
| 772 | if (!pSurfaceFormats) { |
| 773 | return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, |
| 774 | pSurfaceInfo->surface, |
| 775 | pSurfaceFormatCount, nullptr); |
| 776 | } else { |
| 777 | // temp vector for forwarding; we'll marshal it into the pSurfaceFormats |
| 778 | // after the call. |
| 779 | android::Vector<VkSurfaceFormatKHR> surface_formats; |
| 780 | surface_formats.resize(*pSurfaceFormatCount); |
| 781 | VkResult result = GetPhysicalDeviceSurfaceFormatsKHR( |
| 782 | physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount, |
| 783 | &surface_formats.editItemAt(0)); |
| 784 | |
| 785 | if (result == VK_SUCCESS || result == VK_INCOMPLETE) { |
| 786 | // marshal results individually due to stride difference. |
| 787 | // completely ignore any chained extension structs. |
| 788 | uint32_t formats_to_marshal = *pSurfaceFormatCount; |
| 789 | for (uint32_t i = 0u; i < formats_to_marshal; i++) { |
| 790 | pSurfaceFormats[i].surfaceFormat = surface_formats[i]; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | return result; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | VKAPI_ATTR |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 799 | VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev, |
Yiwei Zhang | e4a559c | 2018-02-15 11:27:36 -0800 | [diff] [blame] | 800 | VkSurfaceKHR surface, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 801 | uint32_t* count, |
| 802 | VkPresentModeKHR* modes) { |
Yiwei Zhang | e4a559c | 2018-02-15 11:27:36 -0800 | [diff] [blame] | 803 | int err; |
| 804 | int query_value; |
| 805 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
| 806 | |
| 807 | err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value); |
| 808 | if (err != 0 || query_value < 0) { |
| 809 | ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d", |
| 810 | strerror(-err), err, query_value); |
| 811 | return VK_ERROR_SURFACE_LOST_KHR; |
| 812 | } |
| 813 | uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value); |
| 814 | |
| 815 | err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value); |
| 816 | if (err != 0 || query_value < 0) { |
| 817 | ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d", |
| 818 | strerror(-err), err, query_value); |
| 819 | return VK_ERROR_SURFACE_LOST_KHR; |
| 820 | } |
| 821 | uint32_t max_buffer_count = static_cast<uint32_t>(query_value); |
| 822 | |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 823 | android::Vector<VkPresentModeKHR> present_modes; |
Yiwei Zhang | e4a559c | 2018-02-15 11:27:36 -0800 | [diff] [blame] | 824 | if (min_undequeued_buffers + 1 < max_buffer_count) |
| 825 | present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR); |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 826 | present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR); |
| 827 | |
| 828 | VkPhysicalDevicePresentationPropertiesANDROID present_properties; |
| 829 | if (QueryPresentationProperties(pdev, &present_properties)) { |
| 830 | if (present_properties.sharedImage) { |
| 831 | present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR); |
| 832 | present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | uint32_t num_modes = uint32_t(present_modes.size()); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 837 | |
| 838 | VkResult result = VK_SUCCESS; |
| 839 | if (modes) { |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 840 | if (*count < num_modes) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 841 | result = VK_INCOMPLETE; |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 842 | *count = std::min(*count, num_modes); |
| 843 | std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes); |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 844 | } else { |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 845 | *count = num_modes; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 846 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 847 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 850 | VKAPI_ATTR |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 851 | VkResult GetDeviceGroupPresentCapabilitiesKHR( |
Ian Elliott | cd8ad33 | 2017-10-13 09:21:12 -0600 | [diff] [blame] | 852 | VkDevice, |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 853 | VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) { |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 854 | ALOGV_IF(pDeviceGroupPresentCapabilities->sType != |
| 855 | VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR, |
| 856 | "vkGetDeviceGroupPresentCapabilitiesKHR: invalid " |
| 857 | "VkDeviceGroupPresentCapabilitiesKHR structure type %d", |
| 858 | pDeviceGroupPresentCapabilities->sType); |
| 859 | |
| 860 | memset(pDeviceGroupPresentCapabilities->presentMask, 0, |
| 861 | sizeof(pDeviceGroupPresentCapabilities->presentMask)); |
| 862 | |
| 863 | // assume device group of size 1 |
| 864 | pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0; |
| 865 | pDeviceGroupPresentCapabilities->modes = |
| 866 | VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR; |
| 867 | |
| 868 | return VK_SUCCESS; |
| 869 | } |
| 870 | |
| 871 | VKAPI_ATTR |
| 872 | VkResult GetDeviceGroupSurfacePresentModesKHR( |
Ian Elliott | cd8ad33 | 2017-10-13 09:21:12 -0600 | [diff] [blame] | 873 | VkDevice, |
| 874 | VkSurfaceKHR, |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 875 | VkDeviceGroupPresentModeFlagsKHR* pModes) { |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 876 | *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR; |
| 877 | return VK_SUCCESS; |
| 878 | } |
| 879 | |
| 880 | VKAPI_ATTR |
Ian Elliott | cd8ad33 | 2017-10-13 09:21:12 -0600 | [diff] [blame] | 881 | VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice, |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 882 | VkSurfaceKHR surface, |
| 883 | uint32_t* pRectCount, |
| 884 | VkRect2D* pRects) { |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 885 | if (!pRects) { |
| 886 | *pRectCount = 1; |
| 887 | } else { |
| 888 | uint32_t count = std::min(*pRectCount, 1u); |
| 889 | bool incomplete = *pRectCount < 1; |
| 890 | |
| 891 | *pRectCount = count; |
| 892 | |
| 893 | if (incomplete) { |
| 894 | return VK_INCOMPLETE; |
| 895 | } |
| 896 | |
| 897 | int err; |
| 898 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
| 899 | |
| 900 | int width = 0, height = 0; |
| 901 | err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width); |
| 902 | if (err != 0) { |
| 903 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 904 | strerror(-err), err); |
| 905 | } |
| 906 | err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height); |
| 907 | if (err != 0) { |
| 908 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 909 | strerror(-err), err); |
| 910 | } |
| 911 | |
| 912 | // TODO: Return something better than "whole window" |
| 913 | pRects[0].offset.x = 0; |
| 914 | pRects[0].offset.y = 0; |
| 915 | pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width), |
| 916 | static_cast<uint32_t>(height)}; |
| 917 | } |
| 918 | return VK_SUCCESS; |
| 919 | } |
| 920 | |
| 921 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 922 | VkResult CreateSwapchainKHR(VkDevice device, |
| 923 | const VkSwapchainCreateInfoKHR* create_info, |
| 924 | const VkAllocationCallbacks* allocator, |
| 925 | VkSwapchainKHR* swapchain_handle) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 926 | int err; |
| 927 | VkResult result = VK_SUCCESS; |
| 928 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 929 | ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64 |
| 930 | " minImageCount=%u imageFormat=%u imageColorSpace=%u" |
| 931 | " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u" |
| 932 | " oldSwapchain=0x%" PRIx64, |
| 933 | reinterpret_cast<uint64_t>(create_info->surface), |
| 934 | create_info->minImageCount, create_info->imageFormat, |
| 935 | create_info->imageColorSpace, create_info->imageExtent.width, |
| 936 | create_info->imageExtent.height, create_info->imageUsage, |
| 937 | create_info->preTransform, create_info->presentMode, |
| 938 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 939 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 940 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 941 | allocator = &GetData(device).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 942 | |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 943 | android_pixel_format native_pixel_format = |
| 944 | GetNativePixelFormat(create_info->imageFormat); |
| 945 | android_dataspace native_dataspace = |
| 946 | GetNativeDataspace(create_info->imageColorSpace); |
| 947 | if (native_dataspace == HAL_DATASPACE_UNKNOWN) { |
| 948 | ALOGE( |
| 949 | "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) " |
| 950 | "failed: Unsupported color space", |
| 951 | create_info->imageColorSpace); |
| 952 | return VK_ERROR_INITIALIZATION_FAILED; |
| 953 | } |
| 954 | |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 955 | ALOGV_IF(create_info->imageArrayLayers != 1, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 956 | "swapchain imageArrayLayers=%u not supported", |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 957 | create_info->imageArrayLayers); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 958 | ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 959 | "swapchain preTransform=%#x not supported", |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 960 | create_info->preTransform); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 961 | ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR || |
Chris Forbes | 980ad05 | 2017-01-18 16:55:07 +1300 | [diff] [blame] | 962 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR || |
Chris Forbes | 1d5f68c | 2017-01-31 10:17:01 +1300 | [diff] [blame] | 963 | create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR || |
| 964 | create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR), |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 965 | "swapchain presentMode=%u not supported", |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 966 | create_info->presentMode); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 967 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 968 | Surface& surface = *SurfaceFromHandle(create_info->surface); |
| 969 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 970 | if (surface.swapchain_handle != create_info->oldSwapchain) { |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 971 | ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64 |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 972 | " because it already has active swapchain 0x%" PRIx64 |
| 973 | " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64, |
| 974 | reinterpret_cast<uint64_t>(create_info->surface), |
| 975 | reinterpret_cast<uint64_t>(surface.swapchain_handle), |
| 976 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 977 | return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; |
| 978 | } |
| 979 | if (create_info->oldSwapchain != VK_NULL_HANDLE) |
| 980 | OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain)); |
| 981 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 982 | // -- Reset the native window -- |
| 983 | // The native window might have been used previously, and had its properties |
| 984 | // changed from defaults. That will affect the answer we get for queries |
| 985 | // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we |
| 986 | // attempt such queries. |
| 987 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 988 | // The native window only allows dequeueing all buffers before any have |
| 989 | // been queued, since after that point at least one is assumed to be in |
| 990 | // non-FREE state at any given time. Disconnecting and re-connecting |
| 991 | // orphans the previous buffers, getting us back to the state where we can |
| 992 | // dequeue all buffers. |
| 993 | err = native_window_api_disconnect(surface.window.get(), |
| 994 | NATIVE_WINDOW_API_EGL); |
| 995 | ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)", |
| 996 | strerror(-err), err); |
| 997 | err = |
| 998 | native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL); |
| 999 | ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)", |
| 1000 | strerror(-err), err); |
| 1001 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1002 | err = native_window_set_buffer_count(surface.window.get(), 0); |
| 1003 | if (err != 0) { |
| 1004 | ALOGE("native_window_set_buffer_count(0) failed: %s (%d)", |
| 1005 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1006 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Hrishikesh Manohar | 9b7e453 | 2017-01-10 17:52:11 +0530 | [diff] [blame] | 1009 | int swap_interval = |
| 1010 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1; |
| 1011 | err = surface.window->setSwapInterval(surface.window.get(), swap_interval); |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1012 | if (err != 0) { |
| 1013 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1014 | // errors and translate them to valid Vulkan result codes? |
| 1015 | ALOGE("native_window->setSwapInterval(1) failed: %s (%d)", |
| 1016 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1017 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 1020 | err = native_window_set_shared_buffer_mode(surface.window.get(), false); |
| 1021 | if (err != 0) { |
| 1022 | ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)", |
| 1023 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1024 | return VK_ERROR_SURFACE_LOST_KHR; |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | err = native_window_set_auto_refresh(surface.window.get(), false); |
| 1028 | if (err != 0) { |
| 1029 | ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)", |
| 1030 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1031 | return VK_ERROR_SURFACE_LOST_KHR; |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 1032 | } |
| 1033 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1034 | // -- Configure the native window -- |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1035 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1036 | const auto& dispatch = GetData(device).driver; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1037 | |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 1038 | err = native_window_set_buffers_format(surface.window.get(), |
| 1039 | native_pixel_format); |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 1040 | if (err != 0) { |
| 1041 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1042 | // errors and translate them to valid Vulkan result codes? |
| 1043 | ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)", |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 1044 | native_pixel_format, strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1045 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 1046 | } |
| 1047 | err = native_window_set_buffers_data_space(surface.window.get(), |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 1048 | native_dataspace); |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 1049 | if (err != 0) { |
| 1050 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1051 | // errors and translate them to valid Vulkan result codes? |
| 1052 | ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)", |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 1053 | native_dataspace, strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1054 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 1057 | err = native_window_set_buffers_dimensions( |
| 1058 | surface.window.get(), static_cast<int>(create_info->imageExtent.width), |
| 1059 | static_cast<int>(create_info->imageExtent.height)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1060 | if (err != 0) { |
| 1061 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1062 | // errors and translate them to valid Vulkan result codes? |
| 1063 | ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)", |
| 1064 | create_info->imageExtent.width, create_info->imageExtent.height, |
| 1065 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1066 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 1069 | // VkSwapchainCreateInfo::preTransform indicates the transformation the app |
| 1070 | // applied during rendering. native_window_set_transform() expects the |
| 1071 | // inverse: the transform the app is requesting that the compositor perform |
| 1072 | // during composition. With native windows, pre-transform works by rendering |
| 1073 | // with the same transform the compositor is applying (as in Vulkan), but |
| 1074 | // then requesting the inverse transform, so that when the compositor does |
| 1075 | // it's job the two transforms cancel each other out and the compositor ends |
| 1076 | // up applying an identity transform to the app's buffer. |
| 1077 | err = native_window_set_buffers_transform( |
| 1078 | surface.window.get(), |
| 1079 | InvertTransformToNative(create_info->preTransform)); |
| 1080 | if (err != 0) { |
| 1081 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1082 | // errors and translate them to valid Vulkan result codes? |
| 1083 | ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)", |
| 1084 | InvertTransformToNative(create_info->preTransform), |
| 1085 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1086 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 1089 | err = native_window_set_scaling_mode( |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1090 | surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 1091 | if (err != 0) { |
| 1092 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1093 | // errors and translate them to valid Vulkan result codes? |
| 1094 | ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)", |
| 1095 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1096 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
Chris Forbes | 97ef461 | 2017-03-30 19:37:50 +1300 | [diff] [blame] | 1099 | VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0; |
| 1100 | if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR || |
| 1101 | create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) { |
| 1102 | swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID; |
| 1103 | err = native_window_set_shared_buffer_mode(surface.window.get(), true); |
| 1104 | if (err != 0) { |
| 1105 | ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err); |
| 1106 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) { |
| 1111 | err = native_window_set_auto_refresh(surface.window.get(), true); |
| 1112 | if (err != 0) { |
| 1113 | ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err); |
| 1114 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1115 | } |
| 1116 | } |
| 1117 | |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 1118 | int query_value; |
| 1119 | err = surface.window->query(surface.window.get(), |
| 1120 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 1121 | &query_value); |
| 1122 | if (err != 0 || query_value < 0) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1123 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1124 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 1125 | ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, |
| 1126 | query_value); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1127 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1128 | } |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 1129 | uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1130 | uint32_t num_images = |
| 1131 | (create_info->minImageCount - 1) + min_undequeued_buffers; |
Chris Forbes | 2c8fc75 | 2017-03-17 11:28:32 +1300 | [diff] [blame] | 1132 | |
| 1133 | // Lower layer insists that we have at least two buffers. This is wasteful |
| 1134 | // and we'd like to relax it in the shared case, but not all the pieces are |
| 1135 | // in place for that to work yet. Note we only lie to the lower layer-- we |
| 1136 | // don't want to give the app back a swapchain with extra images (which they |
| 1137 | // can't actually use!). |
| 1138 | err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1139 | if (err != 0) { |
| 1140 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1141 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1142 | ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images, |
| 1143 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1144 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1147 | int32_t legacy_usage = 0; |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 1148 | if (dispatch.GetSwapchainGrallocUsage2ANDROID) { |
Jesse Hall | d1abd74 | 2017-02-09 21:45:51 -0800 | [diff] [blame] | 1149 | uint64_t consumer_usage, producer_usage; |
Courtney Goeltzenleuchter | 894780b | 2017-04-03 16:11:30 -0600 | [diff] [blame] | 1150 | result = dispatch.GetSwapchainGrallocUsage2ANDROID( |
| 1151 | device, create_info->imageFormat, create_info->imageUsage, |
| 1152 | swapchain_image_usage, &consumer_usage, &producer_usage); |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 1153 | if (result != VK_SUCCESS) { |
| 1154 | ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1155 | return VK_ERROR_SURFACE_LOST_KHR; |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 1156 | } |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1157 | legacy_usage = |
Jesse Hall | 7992781 | 2017-03-23 11:03:23 -0700 | [diff] [blame] | 1158 | android_convertGralloc1To0Usage(producer_usage, consumer_usage); |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 1159 | } else if (dispatch.GetSwapchainGrallocUsageANDROID) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1160 | result = dispatch.GetSwapchainGrallocUsageANDROID( |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1161 | device, create_info->imageFormat, create_info->imageUsage, |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1162 | &legacy_usage); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1163 | if (result != VK_SUCCESS) { |
| 1164 | ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1165 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1166 | } |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1167 | } |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1168 | uint64_t native_usage = static_cast<uint64_t>(legacy_usage); |
| 1169 | |
| 1170 | bool createProtectedSwapchain = false; |
| 1171 | if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) { |
| 1172 | createProtectedSwapchain = true; |
| 1173 | native_usage |= BufferUsage::PROTECTED; |
| 1174 | } |
| 1175 | err = native_window_set_usage(surface.window.get(), native_usage); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1176 | if (err != 0) { |
| 1177 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1178 | // errors and translate them to valid Vulkan result codes? |
| 1179 | ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1180 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1181 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1182 | |
| 1183 | // -- Allocate our Swapchain object -- |
| 1184 | // After this point, we must deallocate the swapchain on error. |
| 1185 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1186 | void* mem = allocator->pfnAllocation(allocator->pUserData, |
| 1187 | sizeof(Swapchain), alignof(Swapchain), |
| 1188 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1189 | if (!mem) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1190 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 1191 | Swapchain* swapchain = |
| 1192 | new (mem) Swapchain(surface, num_images, create_info->presentMode); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1193 | |
| 1194 | // -- Dequeue all buffers and create a VkImage for each -- |
| 1195 | // Any failures during or after this must cancel the dequeued buffers. |
| 1196 | |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 1197 | VkSwapchainImageCreateInfoANDROID swapchain_image_create = { |
| 1198 | #pragma clang diagnostic push |
| 1199 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 1200 | .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID, |
| 1201 | #pragma clang diagnostic pop |
| 1202 | .pNext = nullptr, |
| 1203 | .usage = swapchain_image_usage, |
| 1204 | }; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1205 | VkNativeBufferANDROID image_native_buffer = { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1206 | #pragma clang diagnostic push |
| 1207 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 1208 | .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID, |
| 1209 | #pragma clang diagnostic pop |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 1210 | .pNext = &swapchain_image_create, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1211 | }; |
| 1212 | VkImageCreateInfo image_create = { |
| 1213 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 1214 | .pNext = &image_native_buffer, |
| 1215 | .imageType = VK_IMAGE_TYPE_2D, |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 1216 | .format = create_info->imageFormat, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1217 | .extent = {0, 0, 1}, |
| 1218 | .mipLevels = 1, |
Jesse Hall | a15a4bf | 2015-11-19 22:48:02 -0800 | [diff] [blame] | 1219 | .arrayLayers = 1, |
Jesse Hall | 091ed9e | 2015-11-30 00:55:29 -0800 | [diff] [blame] | 1220 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1221 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1222 | .usage = create_info->imageUsage, |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1223 | .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1224 | .sharingMode = create_info->imageSharingMode, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1225 | .queueFamilyIndexCount = create_info->queueFamilyIndexCount, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1226 | .pQueueFamilyIndices = create_info->pQueueFamilyIndices, |
| 1227 | }; |
| 1228 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1229 | for (uint32_t i = 0; i < num_images; i++) { |
| 1230 | Swapchain::Image& img = swapchain->images[i]; |
| 1231 | |
| 1232 | ANativeWindowBuffer* buffer; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1233 | err = surface.window->dequeueBuffer(surface.window.get(), &buffer, |
| 1234 | &img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1235 | if (err != 0) { |
| 1236 | // TODO(jessehall): Improve error reporting. Can we enumerate |
| 1237 | // possible errors and translate them to valid Vulkan result codes? |
| 1238 | ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1239 | result = VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1240 | break; |
| 1241 | } |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 1242 | img.buffer = buffer; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1243 | img.dequeued = true; |
| 1244 | |
| 1245 | image_create.extent = |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 1246 | VkExtent3D{static_cast<uint32_t>(img.buffer->width), |
| 1247 | static_cast<uint32_t>(img.buffer->height), |
| 1248 | 1}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1249 | image_native_buffer.handle = img.buffer->handle; |
| 1250 | image_native_buffer.stride = img.buffer->stride; |
| 1251 | image_native_buffer.format = img.buffer->format; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1252 | image_native_buffer.usage = int(img.buffer->usage); |
Chris Forbes | 8e0c3f5 | 2017-05-19 14:47:29 -0700 | [diff] [blame] | 1253 | android_convertGralloc0To1Usage(int(img.buffer->usage), |
| 1254 | &image_native_buffer.usage2.producer, |
| 1255 | &image_native_buffer.usage2.consumer); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1256 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1257 | result = |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1258 | dispatch.CreateImage(device, &image_create, nullptr, &img.image); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1259 | if (result != VK_SUCCESS) { |
| 1260 | ALOGD("vkCreateImage w/ native buffer failed: %u", result); |
| 1261 | break; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | // -- Cancel all buffers, returning them to the queue -- |
| 1266 | // If an error occurred before, also destroy the VkImage and release the |
| 1267 | // buffer reference. Otherwise, we retain a strong reference to the buffer. |
| 1268 | // |
| 1269 | // TODO(jessehall): The error path here is the same as DestroySwapchain, |
| 1270 | // but not the non-error path. Should refactor/unify. |
Chris Forbes | 31b85c2 | 2018-05-29 15:03:28 -0700 | [diff] [blame] | 1271 | for (uint32_t i = 0; i < num_images; i++) { |
| 1272 | Swapchain::Image& img = swapchain->images[i]; |
| 1273 | if (img.dequeued) { |
| 1274 | if (!swapchain->shared) { |
Chris Forbes | e0ced03 | 2017-03-30 19:44:15 +1300 | [diff] [blame] | 1275 | surface.window->cancelBuffer(surface.window.get(), img.buffer.get(), |
| 1276 | img.dequeue_fence); |
| 1277 | img.dequeue_fence = -1; |
| 1278 | img.dequeued = false; |
| 1279 | } |
Chris Forbes | 31b85c2 | 2018-05-29 15:03:28 -0700 | [diff] [blame] | 1280 | } |
| 1281 | if (result != VK_SUCCESS) { |
| 1282 | if (img.image) |
| 1283 | dispatch.DestroyImage(device, img.image, nullptr); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | if (result != VK_SUCCESS) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1288 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1289 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1290 | return result; |
| 1291 | } |
| 1292 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1293 | surface.swapchain_handle = HandleFromSwapchain(swapchain); |
| 1294 | *swapchain_handle = surface.swapchain_handle; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1295 | return VK_SUCCESS; |
| 1296 | } |
| 1297 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1298 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1299 | void DestroySwapchainKHR(VkDevice device, |
| 1300 | VkSwapchainKHR swapchain_handle, |
| 1301 | const VkAllocationCallbacks* allocator) { |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1302 | const auto& dispatch = GetData(device).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1303 | Swapchain* swapchain = SwapchainFromHandle(swapchain_handle); |
Daniel Koch | d78c2e8 | 2016-12-13 18:45:13 -0500 | [diff] [blame] | 1304 | if (!swapchain) |
| 1305 | return; |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1306 | bool active = swapchain->surface.swapchain_handle == swapchain_handle; |
| 1307 | ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1308 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1309 | if (swapchain->frame_timestamps_enabled) { |
| 1310 | native_window_enable_frame_timestamps(window, false); |
| 1311 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1312 | for (uint32_t i = 0; i < swapchain->num_images; i++) |
| 1313 | ReleaseSwapchainImage(device, window, -1, swapchain->images[i]); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1314 | if (active) |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1315 | swapchain->surface.swapchain_handle = VK_NULL_HANDLE; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1316 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1317 | allocator = &GetData(device).allocator; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1318 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1319 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1322 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1323 | VkResult GetSwapchainImagesKHR(VkDevice, |
| 1324 | VkSwapchainKHR swapchain_handle, |
| 1325 | uint32_t* count, |
| 1326 | VkImage* images) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1327 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1328 | ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle, |
| 1329 | "getting images for non-active swapchain 0x%" PRIx64 |
| 1330 | "; only dequeued image handles are valid", |
| 1331 | reinterpret_cast<uint64_t>(swapchain_handle)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1332 | VkResult result = VK_SUCCESS; |
| 1333 | if (images) { |
| 1334 | uint32_t n = swapchain.num_images; |
| 1335 | if (*count < swapchain.num_images) { |
| 1336 | n = *count; |
| 1337 | result = VK_INCOMPLETE; |
| 1338 | } |
| 1339 | for (uint32_t i = 0; i < n; i++) |
| 1340 | images[i] = swapchain.images[i].image; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 1341 | *count = n; |
| 1342 | } else { |
| 1343 | *count = swapchain.num_images; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1344 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1345 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1348 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1349 | VkResult AcquireNextImageKHR(VkDevice device, |
| 1350 | VkSwapchainKHR swapchain_handle, |
| 1351 | uint64_t timeout, |
| 1352 | VkSemaphore semaphore, |
| 1353 | VkFence vk_fence, |
| 1354 | uint32_t* image_index) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1355 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1356 | ANativeWindow* window = swapchain.surface.window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1357 | VkResult result; |
| 1358 | int err; |
| 1359 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1360 | if (swapchain.surface.swapchain_handle != swapchain_handle) |
| 1361 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 1362 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1363 | ALOGW_IF( |
| 1364 | timeout != UINT64_MAX, |
| 1365 | "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented"); |
| 1366 | |
Chris Forbes | c88409c | 2017-03-30 19:47:37 +1300 | [diff] [blame] | 1367 | if (swapchain.shared) { |
| 1368 | // In shared mode, we keep the buffer dequeued all the time, so we don't |
| 1369 | // want to dequeue a buffer here. Instead, just ask the driver to ensure |
| 1370 | // the semaphore and fence passed to us will be signalled. |
| 1371 | *image_index = 0; |
| 1372 | result = GetData(device).driver.AcquireImageANDROID( |
| 1373 | device, swapchain.images[*image_index].image, -1, semaphore, vk_fence); |
| 1374 | return result; |
| 1375 | } |
| 1376 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1377 | ANativeWindowBuffer* buffer; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1378 | int fence_fd; |
| 1379 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1380 | if (err != 0) { |
| 1381 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1382 | // errors and translate them to valid Vulkan result codes? |
| 1383 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1384 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | uint32_t idx; |
| 1388 | for (idx = 0; idx < swapchain.num_images; idx++) { |
| 1389 | if (swapchain.images[idx].buffer.get() == buffer) { |
| 1390 | swapchain.images[idx].dequeued = true; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1391 | swapchain.images[idx].dequeue_fence = fence_fd; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1392 | break; |
| 1393 | } |
| 1394 | } |
| 1395 | if (idx == swapchain.num_images) { |
| 1396 | ALOGE("dequeueBuffer returned unrecognized buffer"); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1397 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1398 | return VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | int fence_clone = -1; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1402 | if (fence_fd != -1) { |
| 1403 | fence_clone = dup(fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1404 | if (fence_clone == -1) { |
| 1405 | ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", |
| 1406 | strerror(errno), errno); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1407 | sync_wait(fence_fd, -1 /* forever */); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1411 | result = GetData(device).driver.AcquireImageANDROID( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1412 | device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1413 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 1414 | // NOTE: we're relying on AcquireImageANDROID to close fence_clone, |
| 1415 | // even if the call fails. We could close it ourselves on failure, but |
| 1416 | // that would create a race condition if the driver closes it on a |
| 1417 | // failure path: some other thread might create an fd with the same |
| 1418 | // number between the time the driver closes it and the time we close |
| 1419 | // it. We must assume one of: the driver *always* closes it even on |
| 1420 | // failure, or *never* closes it on failure. |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1421 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1422 | swapchain.images[idx].dequeued = false; |
| 1423 | swapchain.images[idx].dequeue_fence = -1; |
| 1424 | return result; |
| 1425 | } |
| 1426 | |
| 1427 | *image_index = idx; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1428 | return VK_SUCCESS; |
| 1429 | } |
| 1430 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1431 | VKAPI_ATTR |
| 1432 | VkResult AcquireNextImage2KHR(VkDevice device, |
| 1433 | const VkAcquireNextImageInfoKHR* pAcquireInfo, |
| 1434 | uint32_t* pImageIndex) { |
| 1435 | // TODO: this should actually be the other way around and this function |
| 1436 | // should handle any additional structures that get passed in |
| 1437 | return AcquireNextImageKHR(device, pAcquireInfo->swapchain, |
| 1438 | pAcquireInfo->timeout, pAcquireInfo->semaphore, |
| 1439 | pAcquireInfo->fence, pImageIndex); |
| 1440 | } |
| 1441 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1442 | static VkResult WorstPresentResult(VkResult a, VkResult b) { |
| 1443 | // See the error ranking for vkQueuePresentKHR at the end of section 29.6 |
| 1444 | // (in spec version 1.0.14). |
| 1445 | static const VkResult kWorstToBest[] = { |
| 1446 | VK_ERROR_DEVICE_LOST, |
| 1447 | VK_ERROR_SURFACE_LOST_KHR, |
| 1448 | VK_ERROR_OUT_OF_DATE_KHR, |
| 1449 | VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 1450 | VK_ERROR_OUT_OF_HOST_MEMORY, |
| 1451 | VK_SUBOPTIMAL_KHR, |
| 1452 | }; |
| 1453 | for (auto result : kWorstToBest) { |
| 1454 | if (a == result || b == result) |
| 1455 | return result; |
| 1456 | } |
| 1457 | ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a); |
| 1458 | ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b); |
| 1459 | return a != VK_SUCCESS ? a : b; |
| 1460 | } |
| 1461 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1462 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1463 | VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1464 | ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
| 1465 | "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d", |
| 1466 | present_info->sType); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1467 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1468 | VkDevice device = GetData(queue).driver_device; |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1469 | const auto& dispatch = GetData(queue).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1470 | VkResult final_result = VK_SUCCESS; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1471 | |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1472 | // Look at the pNext chain for supported extension structs: |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1473 | const VkPresentRegionsKHR* present_regions = nullptr; |
| 1474 | const VkPresentTimesInfoGOOGLE* present_times = nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1475 | const VkPresentRegionsKHR* next = |
| 1476 | reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext); |
| 1477 | while (next) { |
| 1478 | switch (next->sType) { |
| 1479 | case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR: |
| 1480 | present_regions = next; |
| 1481 | break; |
Ian Elliott | 14866bb | 2017-01-20 09:15:48 -0700 | [diff] [blame] | 1482 | case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE: |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1483 | present_times = |
| 1484 | reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next); |
| 1485 | break; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1486 | default: |
| 1487 | ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x", |
| 1488 | next->sType); |
| 1489 | break; |
| 1490 | } |
| 1491 | next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext); |
| 1492 | } |
| 1493 | ALOGV_IF( |
| 1494 | present_regions && |
| 1495 | present_regions->swapchainCount != present_info->swapchainCount, |
| 1496 | "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1497 | ALOGV_IF(present_times && |
| 1498 | present_times->swapchainCount != present_info->swapchainCount, |
| 1499 | "VkPresentTimesInfoGOOGLE::swapchainCount != " |
| 1500 | "VkPresentInfo::swapchainCount"); |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1501 | const VkPresentRegionKHR* regions = |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1502 | (present_regions) ? present_regions->pRegions : nullptr; |
| 1503 | const VkPresentTimeGOOGLE* times = |
| 1504 | (present_times) ? present_times->pTimes : nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1505 | const VkAllocationCallbacks* allocator = &GetData(device).allocator; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1506 | android_native_rect_t* rects = nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1507 | uint32_t nrects = 0; |
| 1508 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1509 | for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) { |
| 1510 | Swapchain& swapchain = |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1511 | *SwapchainFromHandle(present_info->pSwapchains[sc]); |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1512 | uint32_t image_idx = present_info->pImageIndices[sc]; |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 1513 | Swapchain::Image& img = swapchain.images[image_idx]; |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 1514 | const VkPresentRegionKHR* region = |
| 1515 | (regions && !swapchain.mailbox_mode) ? ®ions[sc] : nullptr; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1516 | const VkPresentTimeGOOGLE* time = (times) ? ×[sc] : nullptr; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1517 | VkResult swapchain_result = VK_SUCCESS; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1518 | VkResult result; |
| 1519 | int err; |
| 1520 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1521 | int fence = -1; |
Jesse Hall | 275d76c | 2016-01-08 22:39:16 -0800 | [diff] [blame] | 1522 | result = dispatch.QueueSignalReleaseImageANDROID( |
| 1523 | queue, present_info->waitSemaphoreCount, |
| 1524 | present_info->pWaitSemaphores, img.image, &fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1525 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 1526 | ALOGE("QueueSignalReleaseImageANDROID failed: %d", result); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1527 | swapchain_result = result; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1530 | if (swapchain.surface.swapchain_handle == |
| 1531 | present_info->pSwapchains[sc]) { |
| 1532 | ANativeWindow* window = swapchain.surface.window.get(); |
| 1533 | if (swapchain_result == VK_SUCCESS) { |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1534 | if (region) { |
| 1535 | // Process the incremental-present hint for this swapchain: |
| 1536 | uint32_t rcount = region->rectangleCount; |
| 1537 | if (rcount > nrects) { |
| 1538 | android_native_rect_t* new_rects = |
| 1539 | static_cast<android_native_rect_t*>( |
| 1540 | allocator->pfnReallocation( |
| 1541 | allocator->pUserData, rects, |
| 1542 | sizeof(android_native_rect_t) * rcount, |
| 1543 | alignof(android_native_rect_t), |
| 1544 | VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); |
| 1545 | if (new_rects) { |
| 1546 | rects = new_rects; |
| 1547 | nrects = rcount; |
| 1548 | } else { |
| 1549 | rcount = 0; // Ignore the hint for this swapchain |
| 1550 | } |
| 1551 | } |
| 1552 | for (uint32_t r = 0; r < rcount; ++r) { |
| 1553 | if (region->pRectangles[r].layer > 0) { |
| 1554 | ALOGV( |
| 1555 | "vkQueuePresentKHR ignoring invalid layer " |
| 1556 | "(%u); using layer 0 instead", |
| 1557 | region->pRectangles[r].layer); |
| 1558 | } |
| 1559 | int x = region->pRectangles[r].offset.x; |
| 1560 | int y = region->pRectangles[r].offset.y; |
| 1561 | int width = static_cast<int>( |
| 1562 | region->pRectangles[r].extent.width); |
| 1563 | int height = static_cast<int>( |
| 1564 | region->pRectangles[r].extent.height); |
| 1565 | android_native_rect_t* cur_rect = &rects[r]; |
| 1566 | cur_rect->left = x; |
| 1567 | cur_rect->top = y + height; |
| 1568 | cur_rect->right = x + width; |
| 1569 | cur_rect->bottom = y; |
| 1570 | } |
| 1571 | native_window_set_surface_damage(window, rects, rcount); |
| 1572 | } |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1573 | if (time) { |
| 1574 | if (!swapchain.frame_timestamps_enabled) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1575 | ALOGV( |
| 1576 | "Calling " |
| 1577 | "native_window_enable_frame_timestamps(true)"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1578 | native_window_enable_frame_timestamps(window, true); |
| 1579 | swapchain.frame_timestamps_enabled = true; |
| 1580 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1581 | |
| 1582 | // Record the nativeFrameId so it can be later correlated to |
| 1583 | // this present. |
| 1584 | uint64_t nativeFrameId = 0; |
| 1585 | err = native_window_get_next_frame_id( |
| 1586 | window, &nativeFrameId); |
| 1587 | if (err != android::NO_ERROR) { |
| 1588 | ALOGE("Failed to get next native frame ID."); |
| 1589 | } |
| 1590 | |
| 1591 | // Add a new timing record with the user's presentID and |
| 1592 | // the nativeFrameId. |
| 1593 | swapchain.timing.push_back(TimingInfo(time, nativeFrameId)); |
| 1594 | while (swapchain.timing.size() > MAX_TIMING_INFOS) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1595 | swapchain.timing.removeAt(0); |
| 1596 | } |
| 1597 | if (time->desiredPresentTime) { |
| 1598 | // Set the desiredPresentTime: |
| 1599 | ALOGV( |
| 1600 | "Calling " |
| 1601 | "native_window_set_buffers_timestamp(%" PRId64 ")", |
| 1602 | time->desiredPresentTime); |
| 1603 | native_window_set_buffers_timestamp( |
| 1604 | window, |
| 1605 | static_cast<int64_t>(time->desiredPresentTime)); |
| 1606 | } |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1607 | } |
Chris Forbes | fca0f29 | 2017-03-30 19:48:39 +1300 | [diff] [blame] | 1608 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1609 | err = window->queueBuffer(window, img.buffer.get(), fence); |
| 1610 | // queueBuffer always closes fence, even on error |
| 1611 | if (err != 0) { |
| 1612 | // TODO(jessehall): What now? We should probably cancel the |
| 1613 | // buffer, I guess? |
| 1614 | ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err); |
| 1615 | swapchain_result = WorstPresentResult( |
| 1616 | swapchain_result, VK_ERROR_OUT_OF_DATE_KHR); |
| 1617 | } |
| 1618 | if (img.dequeue_fence >= 0) { |
| 1619 | close(img.dequeue_fence); |
| 1620 | img.dequeue_fence = -1; |
| 1621 | } |
| 1622 | img.dequeued = false; |
Chris Forbes | fca0f29 | 2017-03-30 19:48:39 +1300 | [diff] [blame] | 1623 | |
| 1624 | // If the swapchain is in shared mode, immediately dequeue the |
| 1625 | // buffer so it can be presented again without an intervening |
| 1626 | // call to AcquireNextImageKHR. We expect to get the same buffer |
| 1627 | // back from every call to dequeueBuffer in this mode. |
| 1628 | if (swapchain.shared && swapchain_result == VK_SUCCESS) { |
| 1629 | ANativeWindowBuffer* buffer; |
| 1630 | int fence_fd; |
| 1631 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
| 1632 | if (err != 0) { |
| 1633 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
| 1634 | swapchain_result = WorstPresentResult(swapchain_result, |
| 1635 | VK_ERROR_SURFACE_LOST_KHR); |
| 1636 | } |
| 1637 | else if (img.buffer != buffer) { |
| 1638 | ALOGE("got wrong image back for shared swapchain"); |
| 1639 | swapchain_result = WorstPresentResult(swapchain_result, |
| 1640 | VK_ERROR_SURFACE_LOST_KHR); |
| 1641 | } |
| 1642 | else { |
| 1643 | img.dequeue_fence = fence_fd; |
| 1644 | img.dequeued = true; |
| 1645 | } |
| 1646 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1647 | } |
| 1648 | if (swapchain_result != VK_SUCCESS) { |
| 1649 | ReleaseSwapchainImage(device, window, fence, img); |
| 1650 | OrphanSwapchain(device, &swapchain); |
| 1651 | } |
| 1652 | } else { |
| 1653 | ReleaseSwapchainImage(device, nullptr, fence, img); |
| 1654 | swapchain_result = VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 1657 | if (present_info->pResults) |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1658 | present_info->pResults[sc] = swapchain_result; |
| 1659 | |
| 1660 | if (swapchain_result != final_result) |
| 1661 | final_result = WorstPresentResult(final_result, swapchain_result); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1662 | } |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1663 | if (rects) { |
| 1664 | allocator->pfnFree(allocator->pUserData, rects); |
| 1665 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1666 | |
| 1667 | return final_result; |
| 1668 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1669 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1670 | VKAPI_ATTR |
| 1671 | VkResult GetRefreshCycleDurationGOOGLE( |
| 1672 | VkDevice, |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 1673 | VkSwapchainKHR swapchain_handle, |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1674 | VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) { |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 1675 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1676 | VkResult result = VK_SUCCESS; |
| 1677 | |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 1678 | pDisplayTimingProperties->refreshDuration = |
| 1679 | static_cast<uint64_t>(swapchain.refresh_duration); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1680 | |
| 1681 | return result; |
| 1682 | } |
| 1683 | |
| 1684 | VKAPI_ATTR |
| 1685 | VkResult GetPastPresentationTimingGOOGLE( |
| 1686 | VkDevice, |
| 1687 | VkSwapchainKHR swapchain_handle, |
| 1688 | uint32_t* count, |
| 1689 | VkPastPresentationTimingGOOGLE* timings) { |
| 1690 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
| 1691 | ANativeWindow* window = swapchain.surface.window.get(); |
| 1692 | VkResult result = VK_SUCCESS; |
| 1693 | |
| 1694 | if (!swapchain.frame_timestamps_enabled) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1695 | ALOGV("Calling native_window_enable_frame_timestamps(true)"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1696 | native_window_enable_frame_timestamps(window, true); |
| 1697 | swapchain.frame_timestamps_enabled = true; |
| 1698 | } |
| 1699 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1700 | if (timings) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1701 | // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE) |
| 1702 | copy_ready_timings(swapchain, count, timings); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1703 | } else { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1704 | *count = get_num_ready_timings(swapchain); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | return result; |
| 1708 | } |
| 1709 | |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1710 | VKAPI_ATTR |
| 1711 | VkResult GetSwapchainStatusKHR( |
| 1712 | VkDevice, |
Chris Forbes | 4e18ba8 | 2017-01-20 12:50:17 +1300 | [diff] [blame] | 1713 | VkSwapchainKHR swapchain_handle) { |
| 1714 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1715 | VkResult result = VK_SUCCESS; |
| 1716 | |
Chris Forbes | 4e18ba8 | 2017-01-20 12:50:17 +1300 | [diff] [blame] | 1717 | if (swapchain.surface.swapchain_handle != swapchain_handle) { |
| 1718 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 1719 | } |
| 1720 | |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1721 | // TODO(chrisforbes): Implement this function properly |
| 1722 | |
| 1723 | return result; |
| 1724 | } |
| 1725 | |
Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 1726 | VKAPI_ATTR void SetHdrMetadataEXT( |
Courtney Goeltzenleuchter | 7671d46 | 2018-01-24 11:51:01 -0800 | [diff] [blame] | 1727 | VkDevice, |
Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 1728 | uint32_t swapchainCount, |
| 1729 | const VkSwapchainKHR* pSwapchains, |
| 1730 | const VkHdrMetadataEXT* pHdrMetadataEXTs) { |
Courtney Goeltzenleuchter | 7671d46 | 2018-01-24 11:51:01 -0800 | [diff] [blame] | 1731 | |
| 1732 | for (uint32_t idx = 0; idx < swapchainCount; idx++) { |
| 1733 | Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]); |
| 1734 | if (!swapchain) |
| 1735 | continue; |
| 1736 | |
| 1737 | if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue; |
| 1738 | |
| 1739 | ANativeWindow* window = swapchain->surface.window.get(); |
| 1740 | |
| 1741 | VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx]; |
| 1742 | const android_smpte2086_metadata smpteMetdata = { |
| 1743 | {vulkanMetadata.displayPrimaryRed.x, |
| 1744 | vulkanMetadata.displayPrimaryRed.y}, |
| 1745 | {vulkanMetadata.displayPrimaryGreen.x, |
| 1746 | vulkanMetadata.displayPrimaryGreen.y}, |
| 1747 | {vulkanMetadata.displayPrimaryBlue.x, |
| 1748 | vulkanMetadata.displayPrimaryBlue.y}, |
| 1749 | {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y}, |
| 1750 | vulkanMetadata.maxLuminance, |
| 1751 | vulkanMetadata.minLuminance}; |
| 1752 | native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata); |
| 1753 | |
| 1754 | const android_cta861_3_metadata cta8613Metadata = { |
| 1755 | vulkanMetadata.maxContentLightLevel, |
| 1756 | vulkanMetadata.maxFrameAverageLightLevel}; |
| 1757 | native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata); |
| 1758 | } |
| 1759 | |
Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 1760 | return; |
| 1761 | } |
| 1762 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1763 | } // namespace driver |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1764 | } // namespace vulkan |