xref: /aosp_15_r20/frameworks/native/libs/renderengine/skia/SkiaVkRenderEngine.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2022 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #ifndef SF_SKIAVKRENDERENGINE_H_
18*38e8c45fSAndroid Build Coastguard Worker #define SF_SKIAVKRENDERENGINE_H_
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include "SkiaRenderEngine.h"
21*38e8c45fSAndroid Build Coastguard Worker #include "VulkanInterface.h"
22*38e8c45fSAndroid Build Coastguard Worker #include "compat/SkiaGpuContext.h"
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker namespace android {
25*38e8c45fSAndroid Build Coastguard Worker namespace renderengine {
26*38e8c45fSAndroid Build Coastguard Worker namespace skia {
27*38e8c45fSAndroid Build Coastguard Worker 
28*38e8c45fSAndroid Build Coastguard Worker class SkiaVkRenderEngine : public SkiaRenderEngine {
29*38e8c45fSAndroid Build Coastguard Worker public:
30*38e8c45fSAndroid Build Coastguard Worker     ~SkiaVkRenderEngine() override;
31*38e8c45fSAndroid Build Coastguard Worker 
32*38e8c45fSAndroid Build Coastguard Worker     int getContextPriority() override;
33*38e8c45fSAndroid Build Coastguard Worker 
34*38e8c45fSAndroid Build Coastguard Worker     class DestroySemaphoreInfo {
35*38e8c45fSAndroid Build Coastguard Worker     public:
36*38e8c45fSAndroid Build Coastguard Worker         DestroySemaphoreInfo() = delete;
37*38e8c45fSAndroid Build Coastguard Worker         DestroySemaphoreInfo(const DestroySemaphoreInfo&) = delete;
38*38e8c45fSAndroid Build Coastguard Worker         DestroySemaphoreInfo& operator=(const DestroySemaphoreInfo&) = delete;
39*38e8c45fSAndroid Build Coastguard Worker         DestroySemaphoreInfo& operator=(DestroySemaphoreInfo&&) = delete;
40*38e8c45fSAndroid Build Coastguard Worker 
DestroySemaphoreInfo(VulkanInterface & vulkanInterface,std::vector<VkSemaphore> semaphores)41*38e8c45fSAndroid Build Coastguard Worker         DestroySemaphoreInfo(VulkanInterface& vulkanInterface, std::vector<VkSemaphore> semaphores)
42*38e8c45fSAndroid Build Coastguard Worker               : mVulkanInterface(vulkanInterface), mSemaphores(std::move(semaphores)) {}
DestroySemaphoreInfo(VulkanInterface & vulkanInterface,VkSemaphore semaphore)43*38e8c45fSAndroid Build Coastguard Worker         DestroySemaphoreInfo(VulkanInterface& vulkanInterface, VkSemaphore semaphore)
44*38e8c45fSAndroid Build Coastguard Worker               : DestroySemaphoreInfo(vulkanInterface, std::vector<VkSemaphore>(1, semaphore)) {}
45*38e8c45fSAndroid Build Coastguard Worker 
unref()46*38e8c45fSAndroid Build Coastguard Worker         void unref() {
47*38e8c45fSAndroid Build Coastguard Worker             --mRefs;
48*38e8c45fSAndroid Build Coastguard Worker             if (!mRefs) {
49*38e8c45fSAndroid Build Coastguard Worker                 for (VkSemaphore semaphore : mSemaphores) {
50*38e8c45fSAndroid Build Coastguard Worker                     mVulkanInterface.destroySemaphore(semaphore);
51*38e8c45fSAndroid Build Coastguard Worker                 }
52*38e8c45fSAndroid Build Coastguard Worker                 delete this;
53*38e8c45fSAndroid Build Coastguard Worker             }
54*38e8c45fSAndroid Build Coastguard Worker         }
55*38e8c45fSAndroid Build Coastguard Worker 
56*38e8c45fSAndroid Build Coastguard Worker     private:
57*38e8c45fSAndroid Build Coastguard Worker         ~DestroySemaphoreInfo() = default;
58*38e8c45fSAndroid Build Coastguard Worker 
59*38e8c45fSAndroid Build Coastguard Worker         VulkanInterface& mVulkanInterface;
60*38e8c45fSAndroid Build Coastguard Worker         std::vector<VkSemaphore> mSemaphores;
61*38e8c45fSAndroid Build Coastguard Worker         // We need to make sure we don't delete the VkSemaphore until it is done being used by both
62*38e8c45fSAndroid Build Coastguard Worker         // Skia (including by the GPU) and inside SkiaVkRenderEngine. So we always start with two
63*38e8c45fSAndroid Build Coastguard Worker         // refs, one owned by Skia and one owned by the SkiaVkRenderEngine. The refs are decremented
64*38e8c45fSAndroid Build Coastguard Worker         // each time unref() is called on this object. Skia will call unref() once it is done with
65*38e8c45fSAndroid Build Coastguard Worker         // the semaphore and the GPU has finished work on the semaphore. SkiaVkRenderEngine calls
66*38e8c45fSAndroid Build Coastguard Worker         // unref() after sending the semaphore to Skia and exporting it if need be.
67*38e8c45fSAndroid Build Coastguard Worker         int mRefs = 2;
68*38e8c45fSAndroid Build Coastguard Worker     };
69*38e8c45fSAndroid Build Coastguard Worker 
70*38e8c45fSAndroid Build Coastguard Worker protected:
71*38e8c45fSAndroid Build Coastguard Worker     virtual std::unique_ptr<SkiaGpuContext> createContext(VulkanInterface& vulkanInterface) = 0;
72*38e8c45fSAndroid Build Coastguard Worker     // Redeclare parent functions that Ganesh vs. Graphite subclasses must implement.
73*38e8c45fSAndroid Build Coastguard Worker     virtual void waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) override = 0;
74*38e8c45fSAndroid Build Coastguard Worker     virtual base::unique_fd flushAndSubmit(SkiaGpuContext* context,
75*38e8c45fSAndroid Build Coastguard Worker                                            sk_sp<SkSurface> dstSurface) override = 0;
76*38e8c45fSAndroid Build Coastguard Worker 
77*38e8c45fSAndroid Build Coastguard Worker     SkiaVkRenderEngine(const RenderEngineCreationArgs& args);
78*38e8c45fSAndroid Build Coastguard Worker 
79*38e8c45fSAndroid Build Coastguard Worker     // Implementations of abstract SkiaRenderEngine functions specific to
80*38e8c45fSAndroid Build Coastguard Worker     // Vulkan, but shareable between Ganesh and Graphite.
81*38e8c45fSAndroid Build Coastguard Worker     SkiaRenderEngine::Contexts createContexts() override;
82*38e8c45fSAndroid Build Coastguard Worker     bool supportsProtectedContentImpl() const override;
83*38e8c45fSAndroid Build Coastguard Worker     bool useProtectedContextImpl(GrProtected isProtected) override;
84*38e8c45fSAndroid Build Coastguard Worker     virtual void appendBackendSpecificInfoToDump(std::string& result) override;
85*38e8c45fSAndroid Build Coastguard Worker 
86*38e8c45fSAndroid Build Coastguard Worker     // TODO: b/300533018 - refactor this to be non-static
87*38e8c45fSAndroid Build Coastguard Worker     static VulkanInterface& getVulkanInterface(bool protectedContext);
88*38e8c45fSAndroid Build Coastguard Worker };
89*38e8c45fSAndroid Build Coastguard Worker 
90*38e8c45fSAndroid Build Coastguard Worker } // namespace skia
91*38e8c45fSAndroid Build Coastguard Worker } // namespace renderengine
92*38e8c45fSAndroid Build Coastguard Worker } // namespace android
93*38e8c45fSAndroid Build Coastguard Worker 
94*38e8c45fSAndroid Build Coastguard Worker #endif
95