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