blob: 0a2f9b2228b69013a5713f92822701abbff5ffa2 [file] [log] [blame]
Ian Elliott1f0911e2022-09-09 16:31:47 -06001/*
2 * Copyright 2022 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#ifndef SF_SKIAVKRENDERENGINE_H_
18#define SF_SKIAVKRENDERENGINE_H_
19
20#include <vk/GrVkBackendContext.h>
21
22#include "SkiaRenderEngine.h"
Nolan Scobief52ad202024-03-06 18:18:28 -050023#include "VulkanInterface.h"
Nolan Scobiefc125ec2024-03-11 20:08:27 -040024#include "compat/SkiaGpuContext.h"
Ian Elliott1f0911e2022-09-09 16:31:47 -060025
26namespace android {
27namespace renderengine {
28namespace skia {
29
30class SkiaVkRenderEngine : public SkiaRenderEngine {
31public:
Ian Elliott1f0911e2022-09-09 16:31:47 -060032 ~SkiaVkRenderEngine() override;
33
34 int getContextPriority() override;
35
Nolan Scobief52ad202024-03-06 18:18:28 -050036 class DestroySemaphoreInfo {
37 public:
38 DestroySemaphoreInfo() = delete;
39 DestroySemaphoreInfo(const DestroySemaphoreInfo&) = delete;
40 DestroySemaphoreInfo& operator=(const DestroySemaphoreInfo&) = delete;
41 DestroySemaphoreInfo& operator=(DestroySemaphoreInfo&&) = delete;
42
43 DestroySemaphoreInfo(VulkanInterface& vulkanInterface, std::vector<VkSemaphore> semaphores)
44 : mVulkanInterface(vulkanInterface), mSemaphores(std::move(semaphores)) {}
45 DestroySemaphoreInfo(VulkanInterface& vulkanInterface, VkSemaphore semaphore)
46 : DestroySemaphoreInfo(vulkanInterface, std::vector<VkSemaphore>(1, semaphore)) {}
47
48 void unref() {
49 --mRefs;
50 if (!mRefs) {
51 for (VkSemaphore semaphore : mSemaphores) {
52 mVulkanInterface.destroySemaphore(semaphore);
53 }
54 delete this;
55 }
56 }
57
58 private:
59 ~DestroySemaphoreInfo() = default;
60
61 VulkanInterface& mVulkanInterface;
62 std::vector<VkSemaphore> mSemaphores;
63 // We need to make sure we don't delete the VkSemaphore until it is done being used by both
64 // Skia (including by the GPU) and inside SkiaVkRenderEngine. So we always start with two
65 // refs, one owned by Skia and one owned by the SkiaVkRenderEngine. The refs are decremented
66 // each time unref() is called on this object. Skia will call unref() once it is done with
67 // the semaphore and the GPU has finished work on the semaphore. SkiaVkRenderEngine calls
68 // unref() after sending the semaphore to Skia and exporting it if need be.
69 int mRefs = 2;
70 };
71
Ian Elliott1f0911e2022-09-09 16:31:47 -060072protected:
Nolan Scobie342d3942024-03-21 16:41:39 -040073 virtual std::unique_ptr<SkiaGpuContext> createContext(VulkanInterface& vulkanInterface) = 0;
Nolan Scobie02c160c2024-03-18 10:40:23 -040074 // Redeclare parent functions that Ganesh vs. Graphite subclasses must implement.
75 virtual void waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) override = 0;
Nolan Scobie1e06f2d2024-03-21 14:56:38 -040076 virtual base::unique_fd flushAndSubmit(SkiaGpuContext* context,
77 sk_sp<SkSurface> dstSurface) override = 0;
Nolan Scobie02c160c2024-03-18 10:40:23 -040078
79 SkiaVkRenderEngine(const RenderEngineCreationArgs& args);
80
Ian Elliott1f0911e2022-09-09 16:31:47 -060081 // Implementations of abstract SkiaRenderEngine functions specific to
Nolan Scobie02c160c2024-03-18 10:40:23 -040082 // Vulkan, but shareable between Ganesh and Graphite.
Nolan Scobiefc125ec2024-03-11 20:08:27 -040083 SkiaRenderEngine::Contexts createContexts() override;
Ian Elliott1f0911e2022-09-09 16:31:47 -060084 bool supportsProtectedContentImpl() const override;
85 bool useProtectedContextImpl(GrProtected isProtected) override;
Ian Elliott1f0911e2022-09-09 16:31:47 -060086 void appendBackendSpecificInfoToDump(std::string& result) override;
87
Nolan Scobie02c160c2024-03-18 10:40:23 -040088 // TODO: b/300533018 - refactor this to be non-static
89 static VulkanInterface& getVulkanInterface(bool protectedContext);
Ian Elliott1f0911e2022-09-09 16:31:47 -060090};
91
92} // namespace skia
93} // namespace renderengine
94} // namespace android
95
Ian Elliottbe705072023-01-04 08:31:37 -070096#endif