blob: 8a3987ace8f07da34816f1e09a67658a29c0fa7a [file] [log] [blame]
Ronghua Wu231c3d12015-03-11 15:10:32 -07001/*
Ronghua Wu67e7f542015-03-13 10:47:08 -07002 * Copyright 2015 The Android Open Source Project
Ronghua Wu231c3d12015-03-11 15:10:32 -07003 *
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//#define LOG_NDEBUG 0
18#define LOG_TAG "ResourceManagerService_test"
19#include <utils/Log.h>
20
21#include <gtest/gtest.h>
22
23#include "ResourceManagerService.h"
24#include <media/IResourceManagerService.h>
25#include <media/MediaResource.h>
26#include <media/MediaResourcePolicy.h>
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/ProcessInfoInterface.h>
29
30namespace android {
31
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070032static int64_t getId(const sp<IResourceManagerClient>& client) {
Ronghua Wu37c89242015-07-15 12:23:48 -070033 return (int64_t) client.get();
34}
35
Ronghua Wu231c3d12015-03-11 15:10:32 -070036struct TestProcessInfo : public ProcessInfoInterface {
37 TestProcessInfo() {}
38 virtual ~TestProcessInfo() {}
39
40 virtual bool getPriority(int pid, int *priority) {
41 // For testing, use pid as priority.
42 // Lower the value higher the priority.
43 *priority = pid;
44 return true;
45 }
46
Ronghua Wud11c43a2016-01-27 16:26:12 -080047 virtual bool isValidPid(int /* pid */) {
48 return true;
49 }
50
Ronghua Wu231c3d12015-03-11 15:10:32 -070051private:
52 DISALLOW_EVIL_CONSTRUCTORS(TestProcessInfo);
53};
54
55struct TestClient : public BnResourceManagerClient {
Ronghua Wu37c89242015-07-15 12:23:48 -070056 TestClient(int pid, sp<ResourceManagerService> service)
57 : mReclaimed(false), mPid(pid), mService(service) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -070058
59 virtual bool reclaimResource() {
60 sp<IResourceManagerClient> client(this);
Ronghua Wu37c89242015-07-15 12:23:48 -070061 mService->removeResource(mPid, (int64_t) client.get());
Ronghua Wu231c3d12015-03-11 15:10:32 -070062 mReclaimed = true;
63 return true;
64 }
65
Ronghua Wu8f9dd872015-04-23 15:24:25 -070066 virtual String8 getName() {
67 return String8("test_client");
68 }
69
Ronghua Wu231c3d12015-03-11 15:10:32 -070070 bool reclaimed() const {
71 return mReclaimed;
72 }
73
74 void reset() {
75 mReclaimed = false;
76 }
77
78protected:
79 virtual ~TestClient() {}
80
81private:
82 bool mReclaimed;
Ronghua Wu37c89242015-07-15 12:23:48 -070083 int mPid;
Ronghua Wu231c3d12015-03-11 15:10:32 -070084 sp<ResourceManagerService> mService;
85 DISALLOW_EVIL_CONSTRUCTORS(TestClient);
86};
87
88static const int kTestPid1 = 30;
Chong Zhangee33d642019-08-08 14:26:43 -070089static const int kTestUid1 = 1010;
90
Ronghua Wu231c3d12015-03-11 15:10:32 -070091static const int kTestPid2 = 20;
Chong Zhangee33d642019-08-08 14:26:43 -070092static const int kTestUid2 = 1011;
Ronghua Wu231c3d12015-03-11 15:10:32 -070093
Ronghua Wu05d89f12015-07-07 16:47:42 -070094static const int kLowPriorityPid = 40;
95static const int kMidPriorityPid = 25;
96static const int kHighPriorityPid = 10;
97
Ronghua Wu231c3d12015-03-11 15:10:32 -070098class ResourceManagerServiceTest : public ::testing::Test {
99public:
100 ResourceManagerServiceTest()
101 : mService(new ResourceManagerService(new TestProcessInfo)),
Ronghua Wu37c89242015-07-15 12:23:48 -0700102 mTestClient1(new TestClient(kTestPid1, mService)),
103 mTestClient2(new TestClient(kTestPid2, mService)),
104 mTestClient3(new TestClient(kTestPid2, mService)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700105 }
106
107protected:
108 static bool isEqualResources(const Vector<MediaResource> &resources1,
109 const Vector<MediaResource> &resources2) {
110 if (resources1.size() != resources2.size()) {
111 return false;
112 }
113 for (size_t i = 0; i < resources1.size(); ++i) {
114 if (resources1[i] != resources2[i]) {
115 return false;
116 }
117 }
118 return true;
119 }
120
Chong Zhangee33d642019-08-08 14:26:43 -0700121 static void expectEqResourceInfo(const ResourceInfo &info,
122 int uid,
123 sp<IResourceManagerClient> client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700124 const Vector<MediaResource> &resources) {
Chong Zhangee33d642019-08-08 14:26:43 -0700125 EXPECT_EQ(uid, info.uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700126 EXPECT_EQ(client, info.client);
127 EXPECT_TRUE(isEqualResources(resources, info.resources));
128 }
129
130 void verifyClients(bool c1, bool c2, bool c3) {
131 TestClient *client1 = static_cast<TestClient*>(mTestClient1.get());
132 TestClient *client2 = static_cast<TestClient*>(mTestClient2.get());
133 TestClient *client3 = static_cast<TestClient*>(mTestClient3.get());
134
135 EXPECT_EQ(c1, client1->reclaimed());
136 EXPECT_EQ(c2, client2->reclaimed());
137 EXPECT_EQ(c3, client3->reclaimed());
138
139 client1->reset();
140 client2->reset();
141 client3->reset();
142 }
143
Ronghua Wu67e7f542015-03-13 10:47:08 -0700144 // test set up
145 // ---------------------------------------------------------------------------------
146 // pid priority client type number
147 // ---------------------------------------------------------------------------------
148 // kTestPid1(30) 30 mTestClient1 secure codec 1
149 // graphic memory 200
150 // graphic memory 200
151 // ---------------------------------------------------------------------------------
152 // kTestPid2(20) 20 mTestClient2 non-secure codec 1
153 // graphic memory 300
154 // -------------------------------------------
155 // mTestClient3 secure codec 1
156 // graphic memory 100
157 // ---------------------------------------------------------------------------------
Ronghua Wu231c3d12015-03-11 15:10:32 -0700158 void addResource() {
159 // kTestPid1 mTestClient1
160 Vector<MediaResource> resources1;
Ronghua Wuea15fd22016-03-03 13:35:05 -0800161 resources1.push_back(MediaResource(MediaResource::kSecureCodec, 1));
Chong Zhangee33d642019-08-08 14:26:43 -0700162 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources1);
Ronghua Wuea15fd22016-03-03 13:35:05 -0800163 resources1.push_back(MediaResource(MediaResource::kGraphicMemory, 200));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700164 Vector<MediaResource> resources11;
Ronghua Wuea15fd22016-03-03 13:35:05 -0800165 resources11.push_back(MediaResource(MediaResource::kGraphicMemory, 200));
Chong Zhangee33d642019-08-08 14:26:43 -0700166 mService->addResource(kTestPid1, kTestUid1, getId(mTestClient1), mTestClient1, resources11);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700167
168 // kTestPid2 mTestClient2
169 Vector<MediaResource> resources2;
Ronghua Wuea15fd22016-03-03 13:35:05 -0800170 resources2.push_back(MediaResource(MediaResource::kNonSecureCodec, 1));
171 resources2.push_back(MediaResource(MediaResource::kGraphicMemory, 300));
Chong Zhangee33d642019-08-08 14:26:43 -0700172 mService->addResource(kTestPid2, kTestUid2, getId(mTestClient2), mTestClient2, resources2);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700173
174 // kTestPid2 mTestClient3
175 Vector<MediaResource> resources3;
Chong Zhangee33d642019-08-08 14:26:43 -0700176 mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3);
Ronghua Wuea15fd22016-03-03 13:35:05 -0800177 resources3.push_back(MediaResource(MediaResource::kSecureCodec, 1));
178 resources3.push_back(MediaResource(MediaResource::kGraphicMemory, 100));
Chong Zhangee33d642019-08-08 14:26:43 -0700179 mService->addResource(kTestPid2, kTestUid2, getId(mTestClient3), mTestClient3, resources3);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700180
181 const PidResourceInfosMap &map = mService->mMap;
182 EXPECT_EQ(2u, map.size());
183 ssize_t index1 = map.indexOfKey(kTestPid1);
184 ASSERT_GE(index1, 0);
185 const ResourceInfos &infos1 = map[index1];
186 EXPECT_EQ(1u, infos1.size());
Chong Zhangee33d642019-08-08 14:26:43 -0700187 expectEqResourceInfo(infos1[0], kTestUid1, mTestClient1, resources1);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700188
189 ssize_t index2 = map.indexOfKey(kTestPid2);
190 ASSERT_GE(index2, 0);
191 const ResourceInfos &infos2 = map[index2];
192 EXPECT_EQ(2u, infos2.size());
Chong Zhangee33d642019-08-08 14:26:43 -0700193 expectEqResourceInfo(infos2[0], kTestUid2, mTestClient2, resources2);
194 expectEqResourceInfo(infos2[1], kTestUid2, mTestClient3, resources3);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700195 }
196
197 void testConfig() {
198 EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs);
199 EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec);
200
201 Vector<MediaResourcePolicy> policies1;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700202 policies1.push_back(
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700203 MediaResourcePolicy(
204 String8(kPolicySupportsMultipleSecureCodecs),
205 String8("true")));
206 policies1.push_back(
207 MediaResourcePolicy(
208 String8(kPolicySupportsSecureWithNonSecureCodec),
209 String8("false")));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700210 mService->config(policies1);
211 EXPECT_TRUE(mService->mSupportsMultipleSecureCodecs);
212 EXPECT_FALSE(mService->mSupportsSecureWithNonSecureCodec);
213
214 Vector<MediaResourcePolicy> policies2;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700215 policies2.push_back(
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700216 MediaResourcePolicy(
217 String8(kPolicySupportsMultipleSecureCodecs),
218 String8("false")));
219 policies2.push_back(
220 MediaResourcePolicy(
221 String8(kPolicySupportsSecureWithNonSecureCodec),
222 String8("true")));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700223 mService->config(policies2);
224 EXPECT_FALSE(mService->mSupportsMultipleSecureCodecs);
225 EXPECT_TRUE(mService->mSupportsSecureWithNonSecureCodec);
226 }
227
228 void testRemoveResource() {
229 addResource();
230
Ronghua Wu37c89242015-07-15 12:23:48 -0700231 mService->removeResource(kTestPid2, getId(mTestClient2));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700232
233 const PidResourceInfosMap &map = mService->mMap;
234 EXPECT_EQ(2u, map.size());
235 const ResourceInfos &infos1 = map.valueFor(kTestPid1);
236 const ResourceInfos &infos2 = map.valueFor(kTestPid2);
237 EXPECT_EQ(1u, infos1.size());
238 EXPECT_EQ(1u, infos2.size());
239 // mTestClient2 has been removed.
240 EXPECT_EQ(mTestClient3, infos2[0].client);
241 }
242
243 void testGetAllClients() {
244 addResource();
245
Ronghua Wuea15fd22016-03-03 13:35:05 -0800246 MediaResource::Type type = MediaResource::kSecureCodec;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700247 Vector<sp<IResourceManagerClient> > clients;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700248 EXPECT_FALSE(mService->getAllClients_l(kLowPriorityPid, type, &clients));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700249 // some higher priority process (e.g. kTestPid2) owns the resource, so getAllClients_l
250 // will fail.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700251 EXPECT_FALSE(mService->getAllClients_l(kMidPriorityPid, type, &clients));
Ronghua Wu05d89f12015-07-07 16:47:42 -0700252 EXPECT_TRUE(mService->getAllClients_l(kHighPriorityPid, type, &clients));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700253
254 EXPECT_EQ(2u, clients.size());
255 EXPECT_EQ(mTestClient3, clients[0]);
256 EXPECT_EQ(mTestClient1, clients[1]);
257 }
258
259 void testReclaimResourceSecure() {
260 Vector<MediaResource> resources;
Ronghua Wuea15fd22016-03-03 13:35:05 -0800261 resources.push_back(MediaResource(MediaResource::kSecureCodec, 1));
262 resources.push_back(MediaResource(MediaResource::kGraphicMemory, 150));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700263
264 // ### secure codec can't coexist and secure codec can coexist with non-secure codec ###
265 {
266 addResource();
267 mService->mSupportsMultipleSecureCodecs = false;
268 mService->mSupportsSecureWithNonSecureCodec = true;
269
270 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700271 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
272 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700273
274 // reclaim all secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700275 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
276 verifyClients(true /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700277
278 // call again should reclaim one largest graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700279 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
280 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700281
282 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700283 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700284 }
285
286 // ### secure codecs can't coexist and secure codec can't coexist with non-secure codec ###
287 {
288 addResource();
289 mService->mSupportsMultipleSecureCodecs = false;
290 mService->mSupportsSecureWithNonSecureCodec = false;
291
292 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700293 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
294 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700295
296 // reclaim all secure and non-secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700297 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
298 verifyClients(true /* c1 */, true /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700299
300 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700301 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700302 }
303
304
305 // ### secure codecs can coexist but secure codec can't coexist with non-secure codec ###
306 {
307 addResource();
308 mService->mSupportsMultipleSecureCodecs = true;
309 mService->mSupportsSecureWithNonSecureCodec = false;
310
311 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700312 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
313 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700314
315 // reclaim all non-secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700316 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
317 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700318
319 // call again should reclaim one largest graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700320 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
321 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700322
323 // call again should reclaim another largest graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700324 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
325 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700326
327 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700328 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700329 }
330
331 // ### secure codecs can coexist and secure codec can coexist with non-secure codec ###
332 {
333 addResource();
334 mService->mSupportsMultipleSecureCodecs = true;
335 mService->mSupportsSecureWithNonSecureCodec = true;
336
337 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700338 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700339
Ronghua Wu05d89f12015-07-07 16:47:42 -0700340 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700341 // one largest graphic memory from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700342 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700343
344 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700345 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
346 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700347
348 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700349 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
350 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700351
352 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700353 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700354 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700355
356 // ### secure codecs can coexist and secure codec can coexist with non-secure codec ###
357 {
358 addResource();
359 mService->mSupportsMultipleSecureCodecs = true;
360 mService->mSupportsSecureWithNonSecureCodec = true;
361
362 Vector<MediaResource> resources;
Ronghua Wuea15fd22016-03-03 13:35:05 -0800363 resources.push_back(MediaResource(MediaResource::kSecureCodec, 1));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700364
Ronghua Wu05d89f12015-07-07 16:47:42 -0700365 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700366 // secure codec from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700367 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700368
369 // call again should reclaim another secure codec from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700370 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
371 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700372
Ronghua Wu05d89f12015-07-07 16:47:42 -0700373 // no more secure codec, non-secure codec will be reclaimed.
374 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
375 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700376 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700377 }
378
379 void testReclaimResourceNonSecure() {
380 Vector<MediaResource> resources;
Ronghua Wuea15fd22016-03-03 13:35:05 -0800381 resources.push_back(MediaResource(MediaResource::kNonSecureCodec, 1));
382 resources.push_back(MediaResource(MediaResource::kGraphicMemory, 150));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700383
384 // ### secure codec can't coexist with non-secure codec ###
385 {
386 addResource();
387 mService->mSupportsSecureWithNonSecureCodec = false;
388
389 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700390 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
391 EXPECT_FALSE(mService->reclaimResource(kMidPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700392
393 // reclaim all secure codecs
Ronghua Wu05d89f12015-07-07 16:47:42 -0700394 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
395 verifyClients(true /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700396
397 // call again should reclaim one graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700398 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
399 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700400
401 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700402 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700403 }
404
405
406 // ### secure codec can coexist with non-secure codec ###
407 {
408 addResource();
409 mService->mSupportsSecureWithNonSecureCodec = true;
410
411 // priority too low
Ronghua Wu05d89f12015-07-07 16:47:42 -0700412 EXPECT_FALSE(mService->reclaimResource(kLowPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700413
Ronghua Wu05d89f12015-07-07 16:47:42 -0700414 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700415 // one largest graphic memory from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700416 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700417
418 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700419 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
420 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700421
422 // call again should reclaim another graphic memory from lowest process
Ronghua Wu05d89f12015-07-07 16:47:42 -0700423 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
424 verifyClients(false /* c1 */, false /* c2 */, true /* c3 */);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700425
426 // nothing left
Ronghua Wu05d89f12015-07-07 16:47:42 -0700427 EXPECT_FALSE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700428 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700429
430 // ### secure codec can coexist with non-secure codec ###
431 {
432 addResource();
433 mService->mSupportsSecureWithNonSecureCodec = true;
434
435 Vector<MediaResource> resources;
Ronghua Wuea15fd22016-03-03 13:35:05 -0800436 resources.push_back(MediaResource(MediaResource::kNonSecureCodec, 1));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700437
Ronghua Wu05d89f12015-07-07 16:47:42 -0700438 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700439 // one non secure codec from lowest process got reclaimed
Ronghua Wu05d89f12015-07-07 16:47:42 -0700440 verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700441
Ronghua Wu05d89f12015-07-07 16:47:42 -0700442 // no more non-secure codec, secure codec from lowest priority process will be reclaimed
443 EXPECT_TRUE(mService->reclaimResource(kHighPriorityPid, resources));
444 verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700445
Ronghua Wu05d89f12015-07-07 16:47:42 -0700446 // clean up client 3 which still left
Ronghua Wu37c89242015-07-15 12:23:48 -0700447 mService->removeResource(kTestPid2, getId(mTestClient3));
Ronghua Wu67e7f542015-03-13 10:47:08 -0700448 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700449 }
450
451 void testGetLowestPriorityBiggestClient() {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800452 MediaResource::Type type = MediaResource::kGraphicMemory;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700453 sp<IResourceManagerClient> client;
Ronghua Wu05d89f12015-07-07 16:47:42 -0700454 EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700455
456 addResource();
457
Ronghua Wu05d89f12015-07-07 16:47:42 -0700458 EXPECT_FALSE(mService->getLowestPriorityBiggestClient_l(kLowPriorityPid, type, &client));
459 EXPECT_TRUE(mService->getLowestPriorityBiggestClient_l(kHighPriorityPid, type, &client));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700460
Ronghua Wuea15fd22016-03-03 13:35:05 -0800461 // kTestPid1 is the lowest priority process with MediaResource::kGraphicMemory.
462 // mTestClient1 has the largest MediaResource::kGraphicMemory within kTestPid1.
Ronghua Wu231c3d12015-03-11 15:10:32 -0700463 EXPECT_EQ(mTestClient1, client);
464 }
465
466 void testGetLowestPriorityPid() {
467 int pid;
468 int priority;
469 TestProcessInfo processInfo;
470
Ronghua Wuea15fd22016-03-03 13:35:05 -0800471 MediaResource::Type type = MediaResource::kGraphicMemory;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700472 EXPECT_FALSE(mService->getLowestPriorityPid_l(type, &pid, &priority));
473
474 addResource();
475
476 EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority));
477 EXPECT_EQ(kTestPid1, pid);
478 int priority1;
479 processInfo.getPriority(kTestPid1, &priority1);
480 EXPECT_EQ(priority1, priority);
481
Ronghua Wuea15fd22016-03-03 13:35:05 -0800482 type = MediaResource::kNonSecureCodec;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700483 EXPECT_TRUE(mService->getLowestPriorityPid_l(type, &pid, &priority));
484 EXPECT_EQ(kTestPid2, pid);
485 int priority2;
486 processInfo.getPriority(kTestPid2, &priority2);
487 EXPECT_EQ(priority2, priority);
488 }
489
490 void testGetBiggestClient() {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800491 MediaResource::Type type = MediaResource::kGraphicMemory;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700492 sp<IResourceManagerClient> client;
493 EXPECT_FALSE(mService->getBiggestClient_l(kTestPid2, type, &client));
494
495 addResource();
496
497 EXPECT_TRUE(mService->getBiggestClient_l(kTestPid2, type, &client));
498 EXPECT_EQ(mTestClient2, client);
499 }
500
501 void testIsCallingPriorityHigher() {
502 EXPECT_FALSE(mService->isCallingPriorityHigher_l(101, 100));
503 EXPECT_FALSE(mService->isCallingPriorityHigher_l(100, 100));
504 EXPECT_TRUE(mService->isCallingPriorityHigher_l(99, 100));
505 }
506
507 sp<ResourceManagerService> mService;
508 sp<IResourceManagerClient> mTestClient1;
509 sp<IResourceManagerClient> mTestClient2;
510 sp<IResourceManagerClient> mTestClient3;
511};
512
513TEST_F(ResourceManagerServiceTest, config) {
514 testConfig();
515}
516
517TEST_F(ResourceManagerServiceTest, addResource) {
518 addResource();
519}
520
521TEST_F(ResourceManagerServiceTest, removeResource) {
522 testRemoveResource();
523}
524
525TEST_F(ResourceManagerServiceTest, reclaimResource) {
526 testReclaimResourceSecure();
527 testReclaimResourceNonSecure();
528}
529
530TEST_F(ResourceManagerServiceTest, getAllClients_l) {
531 testGetAllClients();
532}
533
534TEST_F(ResourceManagerServiceTest, getLowestPriorityBiggestClient_l) {
535 testGetLowestPriorityBiggestClient();
536}
537
538TEST_F(ResourceManagerServiceTest, getLowestPriorityPid_l) {
539 testGetLowestPriorityPid();
540}
541
542TEST_F(ResourceManagerServiceTest, getBiggestClient_l) {
543 testGetBiggestClient();
544}
545
546TEST_F(ResourceManagerServiceTest, isCallingPriorityHigher_l) {
547 testIsCallingPriorityHigher();
548}
549
550} // namespace android