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