1 /*
2 * Copyright 2024 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 #include "GraphiteVkRenderEngine.h"
18
19 #undef LOG_TAG
20 #define LOG_TAG "RenderEngine"
21
22 #include <include/gpu/GpuTypes.h>
23 #include <include/gpu/graphite/BackendSemaphore.h>
24 #include <include/gpu/graphite/Context.h>
25 #include <include/gpu/graphite/Recording.h>
26 #include <include/gpu/graphite/vk/VulkanGraphiteTypes.h>
27
28 #include <android-base/stringprintf.h>
29 #include <log/log_main.h>
30 #include <sync/sync.h>
31
32 #include <memory>
33 #include <vector>
34
35 namespace android::renderengine::skia {
36
37 using base::StringAppendF;
38
create(const RenderEngineCreationArgs & args)39 std::unique_ptr<GraphiteVkRenderEngine> GraphiteVkRenderEngine::create(
40 const RenderEngineCreationArgs& args) {
41 std::unique_ptr<GraphiteVkRenderEngine> engine(new GraphiteVkRenderEngine(args));
42 engine->ensureContextsCreated();
43
44 if (getVulkanInterface(false).isInitialized()) {
45 ALOGD("GraphiteVkRenderEngine::%s: successfully initialized GraphiteVkRenderEngine",
46 __func__);
47 return engine;
48 } else {
49 ALOGE("GraphiteVkRenderEngine::%s: could not create GraphiteVkRenderEngine. "
50 "Likely insufficient Vulkan support",
51 __func__);
52 return {};
53 }
54 }
55
56 // Graphite-specific function signature for fFinishedProc callback.
unref_semaphore(void * semaphore,skgpu::CallbackResult result)57 static void unref_semaphore(void* semaphore, skgpu::CallbackResult result) {
58 if (result != skgpu::CallbackResult::kSuccess) {
59 ALOGE("Graphite submission of work to GPU failed, check for Skia errors");
60 }
61 SkiaVkRenderEngine::DestroySemaphoreInfo* info =
62 reinterpret_cast<SkiaVkRenderEngine::DestroySemaphoreInfo*>(semaphore);
63 info->unref();
64 }
65
createContext(VulkanInterface & vulkanInterface)66 std::unique_ptr<SkiaGpuContext> GraphiteVkRenderEngine::createContext(
67 VulkanInterface& vulkanInterface) {
68 return SkiaGpuContext::MakeVulkan_Graphite(vulkanInterface.getGraphiteBackendContext());
69 }
70
waitFence(SkiaGpuContext *,base::borrowed_fd fenceFd)71 void GraphiteVkRenderEngine::waitFence(SkiaGpuContext*, base::borrowed_fd fenceFd) {
72 if (fenceFd.get() < 0) return;
73
74 int dupedFd = dup(fenceFd.get());
75 if (dupedFd < 0) {
76 ALOGE("failed to create duplicate fence fd: %d", dupedFd);
77 sync_wait(fenceFd.get(), -1);
78 return;
79 }
80
81 base::unique_fd fenceDup(dupedFd);
82 VkSemaphore waitSemaphore =
83 getVulkanInterface(isProtected()).importSemaphoreFromSyncFd(fenceDup.release());
84 auto beSemaphore = graphite::BackendSemaphores::MakeVulkan(waitSemaphore);
85 mStagedWaitSemaphores.push_back(beSemaphore);
86 }
87
flushAndSubmit(SkiaGpuContext * context,sk_sp<SkSurface>)88 base::unique_fd GraphiteVkRenderEngine::flushAndSubmit(SkiaGpuContext* context, sk_sp<SkSurface>) {
89 // Minimal Recording setup. Required even if there are no incoming semaphores to wait on, and if
90 // creating the outgoing signaling semaphore fails.
91 std::unique_ptr<graphite::Recording> recording = context->graphiteRecorder()->snap();
92 graphite::InsertRecordingInfo insertInfo;
93 insertInfo.fRecording = recording.get();
94
95 VulkanInterface& vulkanInterface = getVulkanInterface(isProtected());
96 // This "signal" semaphore is called after rendering, but it is cleaned up in the same mechanism
97 // as "wait" semaphores from waitFence.
98 VkSemaphore vkSignalSemaphore = vulkanInterface.createExportableSemaphore();
99 auto backendSignalSemaphore = graphite::BackendSemaphores::MakeVulkan(vkSignalSemaphore);
100
101 // Collect all Vk semaphores that DestroySemaphoreInfo needs to own and delete after GPU work.
102 std::vector<VkSemaphore> vkSemaphoresToCleanUp;
103 if (vkSignalSemaphore != VK_NULL_HANDLE) {
104 vkSemaphoresToCleanUp.push_back(vkSignalSemaphore);
105 }
106 for (auto backendWaitSemaphore : mStagedWaitSemaphores) {
107 vkSemaphoresToCleanUp.push_back(
108 graphite::BackendSemaphores::GetVkSemaphore(backendWaitSemaphore));
109 }
110
111 DestroySemaphoreInfo* destroySemaphoreInfo = nullptr;
112 if (vkSemaphoresToCleanUp.size() > 0) {
113 destroySemaphoreInfo =
114 new DestroySemaphoreInfo(vulkanInterface, std::move(vkSemaphoresToCleanUp));
115
116 insertInfo.fNumWaitSemaphores = mStagedWaitSemaphores.size();
117 insertInfo.fWaitSemaphores = mStagedWaitSemaphores.data();
118 insertInfo.fNumSignalSemaphores = 1;
119 insertInfo.fSignalSemaphores = &backendSignalSemaphore;
120 insertInfo.fFinishedProc = unref_semaphore;
121 insertInfo.fFinishedContext = destroySemaphoreInfo;
122 }
123
124 const bool inserted = context->graphiteContext()->insertRecording(insertInfo);
125 LOG_ALWAYS_FATAL_IF(!inserted,
126 "graphite::Context::insertRecording(...) failed, check for Skia errors");
127 const bool submitted = context->graphiteContext()->submit(graphite::SyncToCpu::kNo);
128 LOG_ALWAYS_FATAL_IF(!submitted, "graphite::Context::submit(...) failed, check for Skia errors");
129 // Skia's "backend" semaphores can be deleted immediately after inserting the recording; only
130 // the underlying VK semaphores need to be kept until GPU work is complete.
131 mStagedWaitSemaphores.clear();
132
133 base::unique_fd drawFenceFd(-1);
134 if (vkSignalSemaphore != VK_NULL_HANDLE) {
135 drawFenceFd.reset(vulkanInterface.exportSemaphoreSyncFd(vkSignalSemaphore));
136 }
137 // Now that drawFenceFd has been created, we can delete RE's reference to this semaphore, as
138 // another reference is still held until fFinishedProc is called after completion of GPU work.
139 if (destroySemaphoreInfo) {
140 destroySemaphoreInfo->unref();
141 }
142 return drawFenceFd;
143 }
144
appendBackendSpecificInfoToDump(std::string & result)145 void GraphiteVkRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
146 StringAppendF(&result, "\n ------------RE Vulkan (Graphite)----------\n");
147 SkiaVkRenderEngine::appendBackendSpecificInfoToDump(result);
148 }
149
150 } // namespace android::renderengine::skia
151