Nolan Scobie | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 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 "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 Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame^] | 26 | #include <utils/Trace.h> |
Nolan Scobie | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 27 | |
| 28 | namespace android::renderengine::skia { |
| 29 | |
| 30 | // Ganesh-specific function signature for fFinishedProc callback. |
| 31 | static void unref_semaphore(void* semaphore) { |
| 32 | SkiaVkRenderEngine::DestroySemaphoreInfo* info = |
| 33 | reinterpret_cast<SkiaVkRenderEngine::DestroySemaphoreInfo*>(semaphore); |
| 34 | info->unref(); |
| 35 | } |
| 36 | |
| 37 | void GaneshVkRenderEngine::waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) { |
| 38 | if (fenceFd.get() < 0) return; |
| 39 | |
| 40 | const int dupedFd = dup(fenceFd.get()); |
| 41 | if (dupedFd < 0) { |
| 42 | ALOGE("failed to create duplicate fence fd: %d", dupedFd); |
| 43 | sync_wait(fenceFd.get(), -1); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | base::unique_fd fenceDup(dupedFd); |
| 48 | VkSemaphore waitSemaphore = |
| 49 | getVulkanInterface(isProtected()).importSemaphoreFromSyncFd(fenceDup.release()); |
| 50 | GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(waitSemaphore); |
| 51 | constexpr bool kDeleteAfterWait = true; |
| 52 | context->grDirectContext()->wait(1, &beSemaphore, kDeleteAfterWait); |
| 53 | } |
| 54 | |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame^] | 55 | base::unique_fd GaneshVkRenderEngine::flushAndSubmit(SkiaGpuContext* context, |
| 56 | sk_sp<SkSurface> dstSurface) { |
Nolan Scobie | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 57 | sk_sp<GrDirectContext> grContext = context->grDirectContext(); |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame^] | 58 | { |
| 59 | ATRACE_NAME("flush surface"); |
| 60 | // TODO: Investigate feasibility of combining this "surface flush" into the "context flush" |
| 61 | // below. |
| 62 | context->grDirectContext()->flush(dstSurface.get()); |
| 63 | } |
| 64 | |
Nolan Scobie | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 65 | VulkanInterface& vi = getVulkanInterface(isProtected()); |
| 66 | VkSemaphore semaphore = vi.createExportableSemaphore(); |
Nolan Scobie | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 67 | GrBackendSemaphore backendSemaphore = GrBackendSemaphores::MakeVk(semaphore); |
| 68 | |
| 69 | GrFlushInfo flushInfo; |
| 70 | DestroySemaphoreInfo* destroySemaphoreInfo = nullptr; |
| 71 | if (semaphore != VK_NULL_HANDLE) { |
| 72 | destroySemaphoreInfo = new DestroySemaphoreInfo(vi, semaphore); |
| 73 | flushInfo.fNumSemaphores = 1; |
| 74 | flushInfo.fSignalSemaphores = &backendSemaphore; |
| 75 | flushInfo.fFinishedProc = unref_semaphore; |
| 76 | flushInfo.fFinishedContext = destroySemaphoreInfo; |
| 77 | } |
| 78 | GrSemaphoresSubmitted submitted = grContext->flush(flushInfo); |
| 79 | grContext->submit(GrSyncCpu::kNo); |
| 80 | int drawFenceFd = -1; |
| 81 | if (semaphore != VK_NULL_HANDLE) { |
| 82 | if (GrSemaphoresSubmitted::kYes == submitted) { |
| 83 | drawFenceFd = vi.exportSemaphoreSyncFd(semaphore); |
| 84 | } |
| 85 | // Now that drawFenceFd has been created, we can delete our reference to this semaphore |
| 86 | flushInfo.fFinishedProc(destroySemaphoreInfo); |
| 87 | } |
| 88 | base::unique_fd res(drawFenceFd); |
| 89 | return res; |
| 90 | } |
| 91 | |
| 92 | } // namespace android::renderengine::skia |