1 /*
2 **
3 ** Copyright 2023, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef ANDROID_MEDIA_PROCESSPRIORITYRECLAIMPOLICY_H_
19 #define ANDROID_MEDIA_PROCESSPRIORITYRECLAIMPOLICY_H_
20 
21 #include <media/MediaResource.h>
22 #include "IReclaimPolicy.h"
23 
24 namespace android {
25 
26 class ResourceTracker;
27 struct ClientInfo;
28 
29 /*
30  * Implementation of the Reclaim Policy based on the process priority.
31  *
32  * Find the lowest priority process (lower than the calling/requesting process’s priority)
33  * that has the required resources.
34  * From that process, find the biggest client and return the same for reclaiming.
35  * If there is a codec co-existence policy, that is addressed as below:
36  *   - if these are any conflicting codecs, reclaim all those conflicting clients.
37  * If no conflicting codecs, the reclaim policy will select a client in the order of:
38  *   - Find the biggest client from the lowest priority process that
39  *     has the other resources and with the given primary type.
40  *   - select the biggest client from the lower priority process that
41  *     has the primary type.
42  *   - If it's a codec reclaim request, then:
43  *      - select the biggest client from the lower priority process that
44  *        has the othe type (for example secure for a non-secure and vice versa).
45  */
46 class ProcessPriorityReclaimPolicy : public IReclaimPolicy {
47 public:
48     ProcessPriorityReclaimPolicy(const std::shared_ptr<ResourceTracker>& resourceTracker);
49 
50     virtual ~ProcessPriorityReclaimPolicy();
51 
52     /*
53      * Based on the process priority, identify and return a client from the list
54      * of given clients that satisfy the resource requested.
55      *
56      * @param[in]  reclaimRequestInfo Information about the resource request
57      * @param[in]  client List of clients to select from.
58      * @param[out] targetClients Upon success, this will have the list of identified client(s).
59      *
60      * @return true on success, false otherwise
61      */
62     bool getClients(const ReclaimRequestInfo& reclaimRequestInfo,
63                     const std::vector<ClientInfo>& clients,
64                     std::vector<ClientInfo>& targetClients) override;
65 
66 private:
67 
68     // Get the biggest client with the given resources from the given list of clients.
69     // The client should belong to lowest possible priority than that of the
70     // calling/requesting process.
71     // returns true on success, false otherwise
72     //
73     bool getBiggestClientFromLowestPriority(
74         pid_t callingPid,
75         int callingPriority,
76         MediaResource::Type type,
77         MediaResource::SubType subType,
78         MediaResource::SubType primarySubType,
79         const std::vector<ClientInfo>& clients,
80         ClientInfo& targetClient,
81         int& lowestPriority);
82 
83 private:
84     std::shared_ptr<ResourceTracker> mResourceTracker;
85 };
86 
87 } // namespace android
88 
89 #endif  // ANDROID_MEDIA_PROCESSPRIORITYRECLAIMPOLICY_H_
90