blob: aa18713295960d1c594c91e259c3ef2b2b621130 [file] [log] [blame]
Nolan Scobie02c160c2024-03-18 10:40:23 -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 "GaneshVkRenderEngine.h"
18
19#undef LOG_TAG
20#define LOG_TAG "RenderEngine"
21
22#include <include/gpu/ganesh/vk/GrVkBackendSemaphore.h>
23
24#include <log/log_main.h>
25#include <sync/sync.h>
Nolan Scobie1e06f2d2024-03-21 14:56:38 -040026#include <utils/Trace.h>
Nolan Scobie02c160c2024-03-18 10:40:23 -040027
28namespace android::renderengine::skia {
29
30// Ganesh-specific function signature for fFinishedProc callback.
31static void unref_semaphore(void* semaphore) {
32 SkiaVkRenderEngine::DestroySemaphoreInfo* info =
33 reinterpret_cast<SkiaVkRenderEngine::DestroySemaphoreInfo*>(semaphore);
34 info->unref();
35}
36
Nolan Scobie342d3942024-03-21 16:41:39 -040037std::unique_ptr<SkiaGpuContext> GaneshVkRenderEngine::createContext(
38 VulkanInterface& vulkanInterface) {
39 return SkiaGpuContext::MakeVulkan_Ganesh(vulkanInterface.getGaneshBackendContext(),
40 mSkSLCacheMonitor);
41}
42
Nolan Scobie02c160c2024-03-18 10:40:23 -040043void GaneshVkRenderEngine::waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) {
44 if (fenceFd.get() < 0) return;
45
46 const int dupedFd = dup(fenceFd.get());
47 if (dupedFd < 0) {
48 ALOGE("failed to create duplicate fence fd: %d", dupedFd);
49 sync_wait(fenceFd.get(), -1);
50 return;
51 }
52
53 base::unique_fd fenceDup(dupedFd);
54 VkSemaphore waitSemaphore =
55 getVulkanInterface(isProtected()).importSemaphoreFromSyncFd(fenceDup.release());
56 GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(waitSemaphore);
57 constexpr bool kDeleteAfterWait = true;
58 context->grDirectContext()->wait(1, &beSemaphore, kDeleteAfterWait);
59}
60
Nolan Scobie1e06f2d2024-03-21 14:56:38 -040061base::unique_fd GaneshVkRenderEngine::flushAndSubmit(SkiaGpuContext* context,
62 sk_sp<SkSurface> dstSurface) {
Nolan Scobie02c160c2024-03-18 10:40:23 -040063 sk_sp<GrDirectContext> grContext = context->grDirectContext();
Nolan Scobie1e06f2d2024-03-21 14:56:38 -040064 {
65 ATRACE_NAME("flush surface");
66 // TODO: Investigate feasibility of combining this "surface flush" into the "context flush"
67 // below.
68 context->grDirectContext()->flush(dstSurface.get());
69 }
70
Nolan Scobie02c160c2024-03-18 10:40:23 -040071 VulkanInterface& vi = getVulkanInterface(isProtected());
72 VkSemaphore semaphore = vi.createExportableSemaphore();
Nolan Scobie02c160c2024-03-18 10:40:23 -040073 GrBackendSemaphore backendSemaphore = GrBackendSemaphores::MakeVk(semaphore);
74
75 GrFlushInfo flushInfo;
76 DestroySemaphoreInfo* destroySemaphoreInfo = nullptr;
77 if (semaphore != VK_NULL_HANDLE) {
78 destroySemaphoreInfo = new DestroySemaphoreInfo(vi, semaphore);
79 flushInfo.fNumSemaphores = 1;
80 flushInfo.fSignalSemaphores = &backendSemaphore;
81 flushInfo.fFinishedProc = unref_semaphore;
82 flushInfo.fFinishedContext = destroySemaphoreInfo;
83 }
84 GrSemaphoresSubmitted submitted = grContext->flush(flushInfo);
85 grContext->submit(GrSyncCpu::kNo);
86 int drawFenceFd = -1;
87 if (semaphore != VK_NULL_HANDLE) {
88 if (GrSemaphoresSubmitted::kYes == submitted) {
89 drawFenceFd = vi.exportSemaphoreSyncFd(semaphore);
90 }
91 // Now that drawFenceFd has been created, we can delete our reference to this semaphore
92 flushInfo.fFinishedProc(destroySemaphoreInfo);
93 }
94 base::unique_fd res(drawFenceFd);
95 return res;
96}
97
98} // namespace android::renderengine::skia