• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/gpu/vk/GrVkMemoryReclaimer.h"
17 
18 #include "include/core/SkLog.h"
19 
20 #define VK_CALL(GPU, X) GR_VK_CALL((GPU)->vkInterface(), X)
21 
GrVkMemoryReclaimer(bool enabled,const std::function<void ()> & setThreadPriority)22 GrVkMemoryReclaimer::GrVkMemoryReclaimer(bool enabled, const std::function<void()>& setThreadPriority)
23     : fEnabled(enabled),
24       fSetThreadPriority(setThreadPriority)
25 {
26 }
27 
getThreadPool()28 SkExecutor& GrVkMemoryReclaimer::getThreadPool()
29 {
30     static std::unique_ptr<SkExecutor> executor = ({
31         auto executor = SkExecutor::MakeFIFOThreadPool(1, false);
32         executor->add([fSetThreadPriority {fSetThreadPriority}]() {
33             int err = pthread_setname_np(pthread_self(), "async-reclaimer");
34             if (err) {
35                 SK_LOGE("GrVkMemoryReclaimer::GetThreadPool pthread_setname_np, error = %d", err);
36             }
37 
38             if (fSetThreadPriority) {
39                 fSetThreadPriority();
40             }
41         });
42         std::move(executor);
43     });
44     return *executor;
45 }
46 
addMemoryToWaitQueue(const GrVkGpu * gpu,const GrVkAlloc & alloc,const VkBuffer & buffer)47 bool GrVkMemoryReclaimer::addMemoryToWaitQueue(const GrVkGpu* gpu, const GrVkAlloc& alloc, const VkBuffer& buffer)
48 {
49     if (!fEnabled) {
50         return false;
51     }
52     fWaitQueues.emplace_back(WaitQueueItem{.fGpu = gpu, .fAlloc = alloc, .fType = ItemType::BUFFER, .fBuffer = buffer});
53     if (fWaitQueues.size() > fMemoryCountThreshold) {
54         invokeParallelReclaiming();
55     }
56     return true;
57 }
58 
addMemoryToWaitQueue(const GrVkGpu * gpu,const GrVkAlloc & alloc,const VkImage & image)59 bool GrVkMemoryReclaimer::addMemoryToWaitQueue(const GrVkGpu* gpu, const GrVkAlloc& alloc, const VkImage& image)
60 {
61     if (!fEnabled) {
62         return false;
63     }
64     fWaitQueues.emplace_back(WaitQueueItem{.fGpu = gpu, .fAlloc = alloc, .fType = ItemType::IMAGE, .fImage = image});
65     if (fWaitQueues.size() > fMemoryCountThreshold) {
66         invokeParallelReclaiming();
67     }
68     return true;
69 }
70 
addMemoryToWaitQueue(const GrVkGpu * gpu,const VkImageView & imageView)71 bool GrVkMemoryReclaimer::addMemoryToWaitQueue(const GrVkGpu* gpu, const VkImageView& imageView)
72 {
73     if (!fEnabled) {
74         return false;
75     }
76     fWaitQueues.emplace_back(WaitQueueItem{.fGpu = gpu, .fType = ItemType::IMAGE_VIEW, .fImageView = imageView});
77     if (fWaitQueues.size() > fMemoryCountThreshold) {
78         invokeParallelReclaiming();
79     }
80     return true;
81 }
82 
flushGpuMemoryInWaitQueue()83 void GrVkMemoryReclaimer::flushGpuMemoryInWaitQueue()
84 {
85     if (!fEnabled) {
86         return;
87     }
88     if (!fWaitQueues.size()) {
89         return;
90     }
91     invokeParallelReclaiming();
92 }
93 
invokeParallelReclaiming()94 void GrVkMemoryReclaimer::invokeParallelReclaiming()
95 {
96     getThreadPool().add([freeQueues {std::move(fWaitQueues)}] {
97         for (auto& item : freeQueues) {
98             switch (item.fType) {
99                 case ItemType::BUFFER:
100                     GrVkBuffer::DestroyAndFreeBufferMemory(item.fGpu, item.fAlloc, item.fBuffer);
101                     break;
102                 case ItemType::IMAGE:
103                     GrVkImage::DestroyAndFreeImageMemory(item.fGpu, item.fAlloc, item.fImage);
104                     break;
105                 case ItemType::IMAGE_VIEW:
106                     GrVkImageView::DestroyImageView(item.fGpu, item.fImageView);
107                     break;
108             }
109         }
110     });
111 }