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