1 /*
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"
23 #include "VulkanInterface.h"
24 #include "compat/SkiaGpuContext.h"
25 
26 namespace android {
27 namespace renderengine {
28 namespace skia {
29 
30 class SkiaVkRenderEngine : public SkiaRenderEngine {
31 public:
32     ~SkiaVkRenderEngine() override;
33 
34     int getContextPriority() override;
35 
36     class DestroySemaphoreInfo {
37     public:
38         DestroySemaphoreInfo() = delete;
39         DestroySemaphoreInfo(const DestroySemaphoreInfo&) = delete;
40         DestroySemaphoreInfo& operator=(const DestroySemaphoreInfo&) = delete;
41         DestroySemaphoreInfo& operator=(DestroySemaphoreInfo&&) = delete;
42 
DestroySemaphoreInfo(VulkanInterface & vulkanInterface,std::vector<VkSemaphore> semaphores)43         DestroySemaphoreInfo(VulkanInterface& vulkanInterface, std::vector<VkSemaphore> semaphores)
44               : mVulkanInterface(vulkanInterface), mSemaphores(std::move(semaphores)) {}
DestroySemaphoreInfo(VulkanInterface & vulkanInterface,VkSemaphore semaphore)45         DestroySemaphoreInfo(VulkanInterface& vulkanInterface, VkSemaphore semaphore)
46               : DestroySemaphoreInfo(vulkanInterface, std::vector<VkSemaphore>(1, semaphore)) {}
47 
unref()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 
72 protected:
73     virtual std::unique_ptr<SkiaGpuContext> createContext(VulkanInterface& vulkanInterface) = 0;
74     // Redeclare parent functions that Ganesh vs. Graphite subclasses must implement.
75     virtual void waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) override = 0;
76     virtual base::unique_fd flushAndSubmit(SkiaGpuContext* context,
77                                            sk_sp<SkSurface> dstSurface) override = 0;
78 
79     SkiaVkRenderEngine(const RenderEngineCreationArgs& args);
80 
81     // Implementations of abstract SkiaRenderEngine functions specific to
82     // Vulkan, but shareable between Ganesh and Graphite.
83     SkiaRenderEngine::Contexts createContexts() override;
84     bool supportsProtectedContentImpl() const override;
85     bool useProtectedContextImpl(GrProtected isProtected) override;
86     void appendBackendSpecificInfoToDump(std::string& result) override;
87 
88     // TODO: b/300533018 - refactor this to be non-static
89     static VulkanInterface& getVulkanInterface(bool protectedContext);
90 };
91 
92 } // namespace skia
93 } // namespace renderengine
94 } // namespace android
95 
96 #endif
97