1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef GrVkMemoryReclaimer_DEFINED 17 #define GrVkMemoryReclaimer_DEFINED 18 19 #include "include/gpu/vk/GrVkTypes.h" 20 21 #include "include/core/SkExecutor.h" 22 23 #include "src/gpu/vk/GrVkImage.h" 24 #include "src/gpu/vk/GrVkImageView.h" 25 #include "src/gpu/vk/GrVkBuffer.h" 26 27 enum class ItemType { 28 BUFFER, 29 IMAGE, 30 IMAGE_VIEW 31 }; 32 33 class GrVkMemoryReclaimer { 34 public: 35 struct WaitQueueItem { 36 const GrVkGpu* fGpu; 37 GrVkAlloc fAlloc; 38 ItemType fType; 39 union { 40 VkBuffer fBuffer; 41 VkImage fImage; 42 VkImageView fImageView; 43 }; 44 }; 45 46 SkExecutor& getThreadPool(); 47 GrVkMemoryReclaimer(bool enabled, const std::function<void()>& setThreadPriority); 48 bool addMemoryToWaitQueue(const GrVkGpu* gpu, const GrVkAlloc& alloc, const VkBuffer& buffer); 49 bool addMemoryToWaitQueue(const GrVkGpu* gpu, const GrVkAlloc& alloc, const VkImage& image); 50 bool addMemoryToWaitQueue(const GrVkGpu* gpu, const VkImageView& imageView); 51 void flushGpuMemoryInWaitQueue(); 52 private: 53 void invokeParallelReclaiming(); 54 55 bool fEnabled = false; 56 const int fMemoryCountThreshold = 50; 57 58 std::vector<WaitQueueItem> fWaitQueues; 59 60 const std::function<void()> fSetThreadPriority; 61 }; 62 63 #endif