Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 | |
| 17 | // #define LOG_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "RenderEngine" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
| 22 | #include "SkiaVkRenderEngine.h" |
| 23 | |
| 24 | #include <GrBackendSemaphore.h> |
| 25 | #include <GrContextOptions.h> |
| 26 | #include <vk/GrVkExtensions.h> |
| 27 | #include <vk/GrVkTypes.h> |
Kevin Lubick | 9f20708 | 2024-03-06 13:33:17 +0000 | [diff] [blame] | 28 | #include <include/gpu/ganesh/vk/GrVkBackendSemaphore.h> |
Kevin Lubick | 2fc4911 | 2023-10-13 13:10:48 +0000 | [diff] [blame] | 29 | #include <include/gpu/ganesh/vk/GrVkDirectContext.h> |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 30 | |
| 31 | #include <android-base/stringprintf.h> |
| 32 | #include <gui/TraceUtils.h> |
| 33 | #include <sync/sync.h> |
| 34 | #include <utils/Trace.h> |
| 35 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 36 | #include <memory> |
Nolan Scobie | 0f539a7 | 2024-01-29 10:49:10 -0500 | [diff] [blame] | 37 | #include <string> |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 38 | |
| 39 | #include <vulkan/vulkan.h> |
| 40 | #include "log/log_main.h" |
| 41 | |
| 42 | namespace android { |
| 43 | namespace renderengine { |
| 44 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 45 | static skia::VulkanInterface sVulkanInterface; |
| 46 | static skia::VulkanInterface sProtectedContentVulkanInterface; |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 47 | |
| 48 | static void sSetupVulkanInterface() { |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 49 | if (!sVulkanInterface.isInitialized()) { |
| 50 | sVulkanInterface.init(false /* no protected content */); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 51 | // We will have to abort if non-protected VkDevice creation fails (then nothing works). |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 52 | LOG_ALWAYS_FATAL_IF(!sVulkanInterface.isInitialized(), |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 53 | "Could not initialize Vulkan RenderEngine!"); |
| 54 | } |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 55 | if (!sProtectedContentVulkanInterface.isInitialized()) { |
| 56 | sProtectedContentVulkanInterface.init(true /* protected content */); |
| 57 | if (!sProtectedContentVulkanInterface.isInitialized()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 58 | ALOGE("Could not initialize protected content Vulkan RenderEngine."); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 63 | bool RenderEngine::canSupport(GraphicsApi graphicsApi) { |
| 64 | switch (graphicsApi) { |
| 65 | case GraphicsApi::GL: |
| 66 | return true; |
| 67 | case GraphicsApi::VK: { |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 68 | if (!sVulkanInterface.isInitialized()) { |
| 69 | sVulkanInterface.init(false /* no protected content */); |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 70 | ALOGD("%s: initialized == %s.", __func__, |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 71 | sVulkanInterface.isInitialized() ? "true" : "false"); |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 72 | } |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 73 | return sVulkanInterface.isInitialized(); |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 78 | namespace skia { |
| 79 | |
| 80 | using base::StringAppendF; |
| 81 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 82 | std::unique_ptr<SkiaVkRenderEngine> SkiaVkRenderEngine::create( |
| 83 | const RenderEngineCreationArgs& args) { |
| 84 | std::unique_ptr<SkiaVkRenderEngine> engine(new SkiaVkRenderEngine(args)); |
| 85 | engine->ensureGrContextsCreated(); |
| 86 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 87 | if (sVulkanInterface.isInitialized()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 88 | ALOGD("SkiaVkRenderEngine::%s: successfully initialized SkiaVkRenderEngine", __func__); |
| 89 | return engine; |
| 90 | } else { |
| 91 | ALOGD("SkiaVkRenderEngine::%s: could not create SkiaVkRenderEngine. " |
| 92 | "Likely insufficient Vulkan support", |
| 93 | __func__); |
| 94 | return {}; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | SkiaVkRenderEngine::SkiaVkRenderEngine(const RenderEngineCreationArgs& args) |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 99 | : SkiaRenderEngine(args.threaded, static_cast<PixelFormat>(args.pixelFormat), |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 100 | args.supportsBackgroundBlur) {} |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 101 | |
| 102 | SkiaVkRenderEngine::~SkiaVkRenderEngine() { |
| 103 | finishRenderingAndAbandonContext(); |
| 104 | } |
| 105 | |
| 106 | SkiaRenderEngine::Contexts SkiaVkRenderEngine::createDirectContexts( |
| 107 | const GrContextOptions& options) { |
| 108 | sSetupVulkanInterface(); |
| 109 | |
| 110 | SkiaRenderEngine::Contexts contexts; |
Kevin Lubick | 2fc4911 | 2023-10-13 13:10:48 +0000 | [diff] [blame] | 111 | contexts.first = GrDirectContexts::MakeVulkan(sVulkanInterface.getBackendContext(), options); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 112 | if (supportsProtectedContentImpl()) { |
| 113 | contexts.second = |
Kevin Lubick | 2fc4911 | 2023-10-13 13:10:48 +0000 | [diff] [blame] | 114 | GrDirectContexts::MakeVulkan(sProtectedContentVulkanInterface.getBackendContext(), |
| 115 | options); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return contexts; |
| 119 | } |
| 120 | |
| 121 | bool SkiaVkRenderEngine::supportsProtectedContentImpl() const { |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 122 | return sProtectedContentVulkanInterface.isInitialized(); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool SkiaVkRenderEngine::useProtectedContextImpl(GrProtected) { |
| 126 | return true; |
| 127 | } |
| 128 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 129 | static void unref_semaphore(void* semaphore) { |
| 130 | SkiaVkRenderEngine::DestroySemaphoreInfo* info = |
| 131 | reinterpret_cast<SkiaVkRenderEngine::DestroySemaphoreInfo*>(semaphore); |
| 132 | info->unref(); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static VulkanInterface& getVulkanInterface(bool protectedContext) { |
| 136 | if (protectedContext) { |
| 137 | return sProtectedContentVulkanInterface; |
| 138 | } |
| 139 | return sVulkanInterface; |
| 140 | } |
| 141 | |
| 142 | void SkiaVkRenderEngine::waitFence(GrDirectContext* grContext, base::borrowed_fd fenceFd) { |
| 143 | if (fenceFd.get() < 0) return; |
| 144 | |
| 145 | int dupedFd = dup(fenceFd.get()); |
| 146 | if (dupedFd < 0) { |
| 147 | ALOGE("failed to create duplicate fence fd: %d", dupedFd); |
| 148 | sync_wait(fenceFd.get(), -1); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | base::unique_fd fenceDup(dupedFd); |
| 153 | VkSemaphore waitSemaphore = |
| 154 | getVulkanInterface(isProtected()).importSemaphoreFromSyncFd(fenceDup.release()); |
Kevin Lubick | 9f20708 | 2024-03-06 13:33:17 +0000 | [diff] [blame] | 155 | GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(waitSemaphore); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 156 | grContext->wait(1, &beSemaphore, true /* delete after wait */); |
| 157 | } |
| 158 | |
| 159 | base::unique_fd SkiaVkRenderEngine::flushAndSubmit(GrDirectContext* grContext) { |
Ian Elliott | 98e8716 | 2023-04-14 14:05:19 -0600 | [diff] [blame] | 160 | VulkanInterface& vi = getVulkanInterface(isProtected()); |
| 161 | VkSemaphore semaphore = vi.createExportableSemaphore(); |
| 162 | |
Kevin Lubick | 9f20708 | 2024-03-06 13:33:17 +0000 | [diff] [blame] | 163 | GrBackendSemaphore backendSemaphore = GrBackendSemaphores::MakeVk(semaphore); |
Ian Elliott | 98e8716 | 2023-04-14 14:05:19 -0600 | [diff] [blame] | 164 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 165 | GrFlushInfo flushInfo; |
Ian Elliott | 98e8716 | 2023-04-14 14:05:19 -0600 | [diff] [blame] | 166 | DestroySemaphoreInfo* destroySemaphoreInfo = nullptr; |
| 167 | if (semaphore != VK_NULL_HANDLE) { |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 168 | destroySemaphoreInfo = new DestroySemaphoreInfo(vi, semaphore); |
Ian Elliott | 98e8716 | 2023-04-14 14:05:19 -0600 | [diff] [blame] | 169 | flushInfo.fNumSemaphores = 1; |
| 170 | flushInfo.fSignalSemaphores = &backendSemaphore; |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 171 | flushInfo.fFinishedProc = unref_semaphore; |
Ian Elliott | 98e8716 | 2023-04-14 14:05:19 -0600 | [diff] [blame] | 172 | flushInfo.fFinishedContext = destroySemaphoreInfo; |
| 173 | } |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 174 | GrSemaphoresSubmitted submitted = grContext->flush(flushInfo); |
Kevin Lubick | a0efc34 | 2023-09-20 13:11:52 +0000 | [diff] [blame] | 175 | grContext->submit(GrSyncCpu::kNo); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 176 | int drawFenceFd = -1; |
Ian Elliott | 98e8716 | 2023-04-14 14:05:19 -0600 | [diff] [blame] | 177 | if (semaphore != VK_NULL_HANDLE) { |
| 178 | if (GrSemaphoresSubmitted::kYes == submitted) { |
| 179 | drawFenceFd = vi.exportSemaphoreSyncFd(semaphore); |
| 180 | } |
| 181 | // Now that drawFenceFd has been created, we can delete our reference to this semaphore |
| 182 | flushInfo.fFinishedProc(destroySemaphoreInfo); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 183 | } |
| 184 | base::unique_fd res(drawFenceFd); |
| 185 | return res; |
| 186 | } |
| 187 | |
| 188 | int SkiaVkRenderEngine::getContextPriority() { |
| 189 | // EGL_CONTEXT_PRIORITY_REALTIME_NV |
| 190 | constexpr int kRealtimePriority = 0x3357; |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 191 | if (getVulkanInterface(isProtected()).isRealtimePriority()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 192 | return kRealtimePriority; |
| 193 | } else { |
| 194 | return 0; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void SkiaVkRenderEngine::appendBackendSpecificInfoToDump(std::string& result) { |
| 199 | StringAppendF(&result, "\n ------------RE Vulkan----------\n"); |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 200 | StringAppendF(&result, "\n Vulkan device initialized: %d\n", sVulkanInterface.isInitialized()); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 201 | StringAppendF(&result, "\n Vulkan protected device initialized: %d\n", |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 202 | sProtectedContentVulkanInterface.isInitialized()); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 203 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 204 | if (!sVulkanInterface.isInitialized()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
| 208 | StringAppendF(&result, "\n Instance extensions:\n"); |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 209 | for (const auto& name : sVulkanInterface.getInstanceExtensionNames()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 210 | StringAppendF(&result, "\n %s\n", name.c_str()); |
| 211 | } |
| 212 | |
| 213 | StringAppendF(&result, "\n Device extensions:\n"); |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 214 | for (const auto& name : sVulkanInterface.getDeviceExtensionNames()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 215 | StringAppendF(&result, "\n %s\n", name.c_str()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | } // namespace skia |
| 220 | } // namespace renderengine |
| 221 | } // namespace android |