blob: a9332fa4e1d944754af4a5a543d79b595220bf39 [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
Nolan Scobiea4814642024-10-30 15:15:00 -040028#include <android-base/stringprintf.h>
Nolan Scobie609e5972024-03-20 14:47:34 -040029#include <log/log_main.h>
30#include <sync/sync.h>
31
32#include <memory>
33#include <vector>
34
35namespace android::renderengine::skia {
36
Nolan Scobiea4814642024-10-30 15:15:00 -040037using base::StringAppendF;
38
Nolan Scobie576e77f2024-03-26 10:59:59 -040039std::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
Nolan Scobie609e5972024-03-20 14:47:34 -040056// Graphite-specific function signature for fFinishedProc callback.
57static 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
66std::unique_ptr<SkiaGpuContext> GraphiteVkRenderEngine::createContext(
67 VulkanInterface& vulkanInterface) {
68 return SkiaGpuContext::MakeVulkan_Graphite(vulkanInterface.getGraphiteBackendContext());
69}
70
71void 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());
Kaylee Lubickaa0812b2024-07-29 20:37:39 +000084 auto beSemaphore = graphite::BackendSemaphores::MakeVulkan(waitSemaphore);
Nolan Scobie609e5972024-03-20 14:47:34 -040085 mStagedWaitSemaphores.push_back(beSemaphore);
86}
87
88base::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();
Kaylee Lubickaa0812b2024-07-29 20:37:39 +000099 auto backendSignalSemaphore = graphite::BackendSemaphores::MakeVulkan(vkSignalSemaphore);
Nolan Scobie609e5972024-03-20 14:47:34 -0400100
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) {
Kaylee Lubickaa0812b2024-07-29 20:37:39 +0000107 vkSemaphoresToCleanUp.push_back(
108 graphite::BackendSemaphores::GetVkSemaphore(backendWaitSemaphore));
Nolan Scobie609e5972024-03-20 14:47:34 -0400109 }
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
Nolan Scobiea4814642024-10-30 15:15:00 -0400145void GraphiteVkRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
146 StringAppendF(&result, "\n ------------RE Vulkan (Graphite)----------\n");
147 SkiaVkRenderEngine::appendBackendSpecificInfoToDump(result);
148}
149
Nolan Scobie609e5972024-03-20 14:47:34 -0400150} // namespace android::renderengine::skia