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