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/GrVkBuffer.h"
25 
26 enum class ItemType {
27     BUFFER,
28     IMAGE
29 };
30 
31 class GrVkMemoryReclaimer {
32 public:
33     struct WaitQueueItem {
34         const GrVkGpu* fGpu;
35         GrVkAlloc fAlloc;
36         ItemType fType;
37         union {
38             VkBuffer fBuffer;
39             VkImage fImage;
40         };
41     };
42 
43     SkExecutor& getThreadPool();
44     void setGpuMemoryAsyncReclaimerSwitch(bool enabled);
45     bool addMemoryToWaitQueue(const GrVkGpu* gpu, const GrVkAlloc& alloc, const VkBuffer& buffer);
46     bool addMemoryToWaitQueue(const GrVkGpu* gpu, const GrVkAlloc& alloc, const VkImage& image);
47     void flushGpuMemoryInWaitQueue();
48 private:
49     void invokeParallelReclaiming();
50 
51     bool fEnabled = false;
52     const int fMemoryCountThreshold = 50;
53 
54     std::vector<WaitQueueItem> fWaitQueues;
55 };
56 
57 #endif
58