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