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 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 27 | #include <GrBackendSemaphore.h> |
| 28 | #include <GrContextOptions.h> |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 29 | #include <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 | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 32 | #include <vk/GrVkExtensions.h> |
| 33 | #include <vk/GrVkTypes.h> |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 34 | |
| 35 | #include <android-base/stringprintf.h> |
| 36 | #include <gui/TraceUtils.h> |
| 37 | #include <sync/sync.h> |
| 38 | #include <utils/Trace.h> |
| 39 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 40 | #include <memory> |
Nolan Scobie | 0f539a7 | 2024-01-29 10:49:10 -0500 | [diff] [blame] | 41 | #include <string> |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 42 | |
| 43 | #include <vulkan/vulkan.h> |
| 44 | #include "log/log_main.h" |
| 45 | |
| 46 | namespace android { |
| 47 | namespace renderengine { |
| 48 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 49 | static skia::VulkanInterface sVulkanInterface; |
| 50 | static skia::VulkanInterface sProtectedContentVulkanInterface; |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 51 | |
| 52 | static void sSetupVulkanInterface() { |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 53 | if (!sVulkanInterface.isInitialized()) { |
| 54 | sVulkanInterface.init(false /* no protected content */); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 55 | // 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] | 56 | LOG_ALWAYS_FATAL_IF(!sVulkanInterface.isInitialized(), |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 57 | "Could not initialize Vulkan RenderEngine!"); |
| 58 | } |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 59 | if (!sProtectedContentVulkanInterface.isInitialized()) { |
| 60 | sProtectedContentVulkanInterface.init(true /* protected content */); |
| 61 | if (!sProtectedContentVulkanInterface.isInitialized()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 62 | ALOGE("Could not initialize protected content Vulkan RenderEngine."); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 67 | bool RenderEngine::canSupport(GraphicsApi graphicsApi) { |
| 68 | switch (graphicsApi) { |
| 69 | case GraphicsApi::GL: |
| 70 | return true; |
| 71 | case GraphicsApi::VK: { |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 72 | if (!sVulkanInterface.isInitialized()) { |
| 73 | sVulkanInterface.init(false /* no protected content */); |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 74 | ALOGD("%s: initialized == %s.", __func__, |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 75 | sVulkanInterface.isInitialized() ? "true" : "false"); |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 76 | } |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 77 | return sVulkanInterface.isInitialized(); |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 82 | namespace skia { |
| 83 | |
| 84 | using base::StringAppendF; |
| 85 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 86 | std::unique_ptr<SkiaVkRenderEngine> SkiaVkRenderEngine::create( |
| 87 | const RenderEngineCreationArgs& args) { |
Nolan Scobie | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 88 | // TODO: b/293371537 - Ganesh vs. Graphite subclass based on flag in RenderEngineCreationArgs |
| 89 | std::unique_ptr<SkiaVkRenderEngine> engine(new GaneshVkRenderEngine(args)); |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 90 | engine->ensureContextsCreated(); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 91 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 92 | if (sVulkanInterface.isInitialized()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 93 | ALOGD("SkiaVkRenderEngine::%s: successfully initialized SkiaVkRenderEngine", __func__); |
| 94 | return engine; |
| 95 | } else { |
| 96 | ALOGD("SkiaVkRenderEngine::%s: could not create SkiaVkRenderEngine. " |
| 97 | "Likely insufficient Vulkan support", |
| 98 | __func__); |
| 99 | return {}; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | SkiaVkRenderEngine::SkiaVkRenderEngine(const RenderEngineCreationArgs& args) |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 104 | : SkiaRenderEngine(args.threaded, static_cast<PixelFormat>(args.pixelFormat), |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 105 | args.supportsBackgroundBlur) {} |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 106 | |
| 107 | SkiaVkRenderEngine::~SkiaVkRenderEngine() { |
| 108 | finishRenderingAndAbandonContext(); |
| 109 | } |
| 110 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 111 | SkiaRenderEngine::Contexts SkiaVkRenderEngine::createContexts() { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 112 | sSetupVulkanInterface(); |
| 113 | |
| 114 | SkiaRenderEngine::Contexts contexts; |
Nolan Scobie | 342d394 | 2024-03-21 16:41:39 -0400 | [diff] [blame^] | 115 | contexts.first = createContext(sVulkanInterface); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 116 | if (supportsProtectedContentImpl()) { |
Nolan Scobie | 342d394 | 2024-03-21 16:41:39 -0400 | [diff] [blame^] | 117 | contexts.second = createContext(sProtectedContentVulkanInterface); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return contexts; |
| 121 | } |
| 122 | |
| 123 | bool SkiaVkRenderEngine::supportsProtectedContentImpl() const { |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 124 | return sProtectedContentVulkanInterface.isInitialized(); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool SkiaVkRenderEngine::useProtectedContextImpl(GrProtected) { |
| 128 | return true; |
| 129 | } |
| 130 | |
Nolan Scobie | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 131 | VulkanInterface& SkiaVkRenderEngine::getVulkanInterface(bool protectedContext) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 132 | if (protectedContext) { |
| 133 | return sProtectedContentVulkanInterface; |
| 134 | } |
| 135 | return sVulkanInterface; |
| 136 | } |
| 137 | |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 138 | int SkiaVkRenderEngine::getContextPriority() { |
| 139 | // EGL_CONTEXT_PRIORITY_REALTIME_NV |
| 140 | constexpr int kRealtimePriority = 0x3357; |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 141 | if (getVulkanInterface(isProtected()).isRealtimePriority()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 142 | return kRealtimePriority; |
| 143 | } else { |
| 144 | return 0; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void SkiaVkRenderEngine::appendBackendSpecificInfoToDump(std::string& result) { |
| 149 | StringAppendF(&result, "\n ------------RE Vulkan----------\n"); |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 150 | StringAppendF(&result, "\n Vulkan device initialized: %d\n", sVulkanInterface.isInitialized()); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 151 | StringAppendF(&result, "\n Vulkan protected device initialized: %d\n", |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 152 | sProtectedContentVulkanInterface.isInitialized()); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 153 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 154 | if (!sVulkanInterface.isInitialized()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
| 158 | StringAppendF(&result, "\n Instance extensions:\n"); |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 159 | for (const auto& name : sVulkanInterface.getInstanceExtensionNames()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 160 | StringAppendF(&result, "\n %s\n", name.c_str()); |
| 161 | } |
| 162 | |
| 163 | StringAppendF(&result, "\n Device extensions:\n"); |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 164 | for (const auto& name : sVulkanInterface.getDeviceExtensionNames()) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 165 | StringAppendF(&result, "\n %s\n", name.c_str()); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | } // namespace skia |
| 170 | } // namespace renderengine |
| 171 | } // namespace android |