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 | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 24 | #include "GaneshVkRenderEngine.h" |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 25 | #include "compat/SkiaGpuContext.h" |
| 26 | |
Nolan Scobie | bc3f360 | 2024-08-30 13:51:37 -0400 | [diff] [blame] | 27 | #include <include/gpu/ganesh/GrBackendSemaphore.h> |
| 28 | #include <include/gpu/ganesh/GrContextOptions.h> |
| 29 | #include <include/gpu/ganesh/GrDirectContext.h> |
Kevin Lubick | 9f20708 | 2024-03-06 13:33:17 +0000 | [diff] [blame] | 30 | #include <include/gpu/ganesh/vk/GrVkBackendSemaphore.h> |
Kevin Lubick | 2fc4911 | 2023-10-13 13:10:48 +0000 | [diff] [blame] | 31 | #include <include/gpu/ganesh/vk/GrVkDirectContext.h> |
Nolan Scobie | bc3f360 | 2024-08-30 13:51:37 -0400 | [diff] [blame] | 32 | #include <include/gpu/ganesh/vk/GrVkTypes.h> |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 33 | |
| 34 | #include <android-base/stringprintf.h> |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 35 | #include <common/trace.h> |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 36 | #include <sync/sync.h> |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 37 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 38 | #include <memory> |
Nolan Scobie | 0f539a7 | 2024-01-29 10:49:10 -0500 | [diff] [blame] | 39 | #include <string> |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 40 | |
| 41 | #include <vulkan/vulkan.h> |
| 42 | #include "log/log_main.h" |
| 43 | |
| 44 | namespace android { |
| 45 | namespace renderengine { |
| 46 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 47 | static skia::VulkanInterface sVulkanInterface; |
| 48 | static skia::VulkanInterface sProtectedContentVulkanInterface; |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 49 | |
| 50 | static void sSetupVulkanInterface() { |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 51 | if (!sVulkanInterface.isInitialized()) { |
| 52 | sVulkanInterface.init(false /* no protected content */); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 53 | // 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] | 54 | LOG_ALWAYS_FATAL_IF(!sVulkanInterface.isInitialized(), |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 55 | "Could not initialize Vulkan RenderEngine!"); |
| 56 | } |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 57 | if (!sProtectedContentVulkanInterface.isInitialized()) { |
| 58 | sProtectedContentVulkanInterface.init(true /* protected content */); |
| 59 | if (!sProtectedContentVulkanInterface.isInitialized()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 60 | ALOGE("Could not initialize protected content Vulkan RenderEngine."); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 65 | bool RenderEngine::canSupport(GraphicsApi graphicsApi) { |
| 66 | switch (graphicsApi) { |
| 67 | case GraphicsApi::GL: |
| 68 | return true; |
| 69 | case GraphicsApi::VK: { |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 70 | // Static local variables are initialized once, on first invocation of the function. |
| 71 | static const bool canSupportVulkan = []() { |
| 72 | if (!sVulkanInterface.isInitialized()) { |
| 73 | sVulkanInterface.init(false /* no protected content */); |
| 74 | ALOGD("%s: initialized == %s.", __func__, |
| 75 | sVulkanInterface.isInitialized() ? "true" : "false"); |
| 76 | if (!sVulkanInterface.isInitialized()) { |
| 77 | sVulkanInterface.teardown(); |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | return true; |
| 82 | }(); |
| 83 | return canSupportVulkan; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void RenderEngine::teardown(GraphicsApi graphicsApi) { |
| 89 | switch (graphicsApi) { |
| 90 | case GraphicsApi::GL: |
| 91 | break; |
| 92 | case GraphicsApi::VK: { |
| 93 | if (sVulkanInterface.isInitialized()) { |
| 94 | sVulkanInterface.teardown(); |
| 95 | ALOGD("Tearing down the unprotected VulkanInterface."); |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 96 | } |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 97 | if (sProtectedContentVulkanInterface.isInitialized()) { |
| 98 | sProtectedContentVulkanInterface.teardown(); |
| 99 | ALOGD("Tearing down the protected VulkanInterface."); |
| 100 | } |
| 101 | break; |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 106 | namespace skia { |
| 107 | |
| 108 | using base::StringAppendF; |
| 109 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 110 | SkiaVkRenderEngine::SkiaVkRenderEngine(const RenderEngineCreationArgs& args) |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 111 | : SkiaRenderEngine(args.threaded, static_cast<PixelFormat>(args.pixelFormat), |
Robin Lee | 7338bd9 | 2024-04-04 14:05:07 +0000 | [diff] [blame] | 112 | args.blurAlgorithm) {} |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 113 | |
| 114 | SkiaVkRenderEngine::~SkiaVkRenderEngine() { |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 115 | finishRenderingAndAbandonContexts(); |
| 116 | // Teardown VulkanInterfaces after Skia contexts have been abandoned |
| 117 | teardown(GraphicsApi::VK); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 118 | } |
| 119 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 120 | SkiaRenderEngine::Contexts SkiaVkRenderEngine::createContexts() { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 121 | sSetupVulkanInterface(); |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 122 | // More work would need to be done in order to have multiple RenderEngine instances. In |
| 123 | // particular, they would not be able to share the same VulkanInterface(s). |
| 124 | LOG_ALWAYS_FATAL_IF(!sVulkanInterface.takeOwnership(), |
| 125 | "SkiaVkRenderEngine couldn't take ownership of existing unprotected " |
| 126 | "VulkanInterface! Only one SkiaVkRenderEngine instance may exist at a " |
| 127 | "time."); |
| 128 | if (sProtectedContentVulkanInterface.isInitialized()) { |
| 129 | // takeOwnership fails on an uninitialized VulkanInterface, but protected content support is |
| 130 | // optional. |
| 131 | LOG_ALWAYS_FATAL_IF(!sProtectedContentVulkanInterface.takeOwnership(), |
| 132 | "SkiaVkRenderEngine couldn't take ownership of existing protected " |
| 133 | "VulkanInterface! Only one SkiaVkRenderEngine instance may exist at a " |
| 134 | "time."); |
| 135 | } |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 136 | |
| 137 | SkiaRenderEngine::Contexts contexts; |
Nolan Scobie | 342d394 | 2024-03-21 16:41:39 -0400 | [diff] [blame] | 138 | contexts.first = createContext(sVulkanInterface); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 139 | if (supportsProtectedContentImpl()) { |
Nolan Scobie | 342d394 | 2024-03-21 16:41:39 -0400 | [diff] [blame] | 140 | contexts.second = createContext(sProtectedContentVulkanInterface); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return contexts; |
| 144 | } |
| 145 | |
| 146 | bool SkiaVkRenderEngine::supportsProtectedContentImpl() const { |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 147 | return sProtectedContentVulkanInterface.isInitialized(); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | bool SkiaVkRenderEngine::useProtectedContextImpl(GrProtected) { |
| 151 | return true; |
| 152 | } |
| 153 | |
Nolan Scobie | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 154 | VulkanInterface& SkiaVkRenderEngine::getVulkanInterface(bool protectedContext) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 155 | if (protectedContext) { |
| 156 | return sProtectedContentVulkanInterface; |
| 157 | } |
| 158 | return sVulkanInterface; |
| 159 | } |
| 160 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 161 | int SkiaVkRenderEngine::getContextPriority() { |
| 162 | // EGL_CONTEXT_PRIORITY_REALTIME_NV |
| 163 | constexpr int kRealtimePriority = 0x3357; |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 164 | if (getVulkanInterface(isProtected()).isRealtimePriority()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 165 | return kRealtimePriority; |
| 166 | } else { |
| 167 | return 0; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void SkiaVkRenderEngine::appendBackendSpecificInfoToDump(std::string& result) { |
Nolan Scobie | a481464 | 2024-10-30 15:15:00 -0400 | [diff] [blame] | 172 | // Subclasses will prepend a backend-specific name / section header |
| 173 | StringAppendF(&result, "Vulkan device initialized: %d\n", sVulkanInterface.isInitialized()); |
| 174 | StringAppendF(&result, "Vulkan protected device initialized: %d\n", |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 175 | sProtectedContentVulkanInterface.isInitialized()); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 176 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 177 | if (!sVulkanInterface.isInitialized()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
Nolan Scobie | a481464 | 2024-10-30 15:15:00 -0400 | [diff] [blame] | 181 | StringAppendF(&result, "Instance extensions: [\n"); |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 182 | for (const auto& name : sVulkanInterface.getInstanceExtensionNames()) { |
Nolan Scobie | a481464 | 2024-10-30 15:15:00 -0400 | [diff] [blame] | 183 | StringAppendF(&result, " %s\n", name.c_str()); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 184 | } |
Nolan Scobie | a481464 | 2024-10-30 15:15:00 -0400 | [diff] [blame] | 185 | StringAppendF(&result, "]\n"); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 186 | |
Nolan Scobie | a481464 | 2024-10-30 15:15:00 -0400 | [diff] [blame] | 187 | StringAppendF(&result, "Device extensions: [\n"); |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 188 | for (const auto& name : sVulkanInterface.getDeviceExtensionNames()) { |
Nolan Scobie | a481464 | 2024-10-30 15:15:00 -0400 | [diff] [blame] | 189 | StringAppendF(&result, " %s\n", name.c_str()); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 190 | } |
Nolan Scobie | a481464 | 2024-10-30 15:15:00 -0400 | [diff] [blame] | 191 | StringAppendF(&result, "]\n"); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | } // namespace skia |
| 195 | } // namespace renderengine |
| 196 | } // namespace android |