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 "SkiaRenderEngine.h" 21 #include "VulkanInterface.h" 22 #include "compat/SkiaGpuContext.h" 23 24 namespace android { 25 namespace renderengine { 26 namespace skia { 27 28 class SkiaVkRenderEngine : public SkiaRenderEngine { 29 public: 30 ~SkiaVkRenderEngine() override; 31 32 int getContextPriority() override; 33 34 class DestroySemaphoreInfo { 35 public: 36 DestroySemaphoreInfo() = delete; 37 DestroySemaphoreInfo(const DestroySemaphoreInfo&) = delete; 38 DestroySemaphoreInfo& operator=(const DestroySemaphoreInfo&) = delete; 39 DestroySemaphoreInfo& operator=(DestroySemaphoreInfo&&) = delete; 40 DestroySemaphoreInfo(VulkanInterface & vulkanInterface,std::vector<VkSemaphore> semaphores)41 DestroySemaphoreInfo(VulkanInterface& vulkanInterface, std::vector<VkSemaphore> semaphores) 42 : mVulkanInterface(vulkanInterface), mSemaphores(std::move(semaphores)) {} DestroySemaphoreInfo(VulkanInterface & vulkanInterface,VkSemaphore semaphore)43 DestroySemaphoreInfo(VulkanInterface& vulkanInterface, VkSemaphore semaphore) 44 : DestroySemaphoreInfo(vulkanInterface, std::vector<VkSemaphore>(1, semaphore)) {} 45 unref()46 void unref() { 47 --mRefs; 48 if (!mRefs) { 49 for (VkSemaphore semaphore : mSemaphores) { 50 mVulkanInterface.destroySemaphore(semaphore); 51 } 52 delete this; 53 } 54 } 55 56 private: 57 ~DestroySemaphoreInfo() = default; 58 59 VulkanInterface& mVulkanInterface; 60 std::vector<VkSemaphore> mSemaphores; 61 // We need to make sure we don't delete the VkSemaphore until it is done being used by both 62 // Skia (including by the GPU) and inside SkiaVkRenderEngine. So we always start with two 63 // refs, one owned by Skia and one owned by the SkiaVkRenderEngine. The refs are decremented 64 // each time unref() is called on this object. Skia will call unref() once it is done with 65 // the semaphore and the GPU has finished work on the semaphore. SkiaVkRenderEngine calls 66 // unref() after sending the semaphore to Skia and exporting it if need be. 67 int mRefs = 2; 68 }; 69 70 protected: 71 virtual std::unique_ptr<SkiaGpuContext> createContext(VulkanInterface& vulkanInterface) = 0; 72 // Redeclare parent functions that Ganesh vs. Graphite subclasses must implement. 73 virtual void waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) override = 0; 74 virtual base::unique_fd flushAndSubmit(SkiaGpuContext* context, 75 sk_sp<SkSurface> dstSurface) override = 0; 76 77 SkiaVkRenderEngine(const RenderEngineCreationArgs& args); 78 79 // Implementations of abstract SkiaRenderEngine functions specific to 80 // Vulkan, but shareable between Ganesh and Graphite. 81 SkiaRenderEngine::Contexts createContexts() override; 82 bool supportsProtectedContentImpl() const override; 83 bool useProtectedContextImpl(GrProtected isProtected) override; 84 virtual void appendBackendSpecificInfoToDump(std::string& result) override; 85 86 // TODO: b/300533018 - refactor this to be non-static 87 static VulkanInterface& getVulkanInterface(bool protectedContext); 88 }; 89 90 } // namespace skia 91 } // namespace renderengine 92 } // namespace android 93 94 #endif 95