blob: b1106f0d6a71a3b09e3e6544c6e296f881678499 [file] [log] [blame]
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
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#include <gtest/gtest.h>
18
19#include "renderthread/CacheManager.h"
20#include "renderthread/EglManager.h"
21#include "tests/common/TestUtils.h"
22
23using namespace android;
24using namespace android::uirenderer;
25using namespace android::uirenderer::renderthread;
26
27static size_t getCacheUsage(GrContext* grContext) {
28 size_t cacheUsage;
29 grContext->getResourceCacheUsage(nullptr, &cacheUsage);
30 return cacheUsage;
31}
32
33RENDERTHREAD_SKIA_PIPELINE_TEST(CacheManager, trimMemory) {
34 DisplayInfo displayInfo = renderThread.mainDisplayInfo();
35 GrContext* grContext = renderThread.getGrContext();
36 ASSERT_TRUE(grContext != nullptr);
37
John Reck1bcacfd2017-11-03 10:12:19 -070038 // create pairs of offscreen render targets and images until we exceed the
39 // backgroundCacheSizeLimit
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040040 std::vector<sk_sp<SkSurface>> surfaces;
41
42 while (getCacheUsage(grContext) <= renderThread.cacheManager().getBackgroundCacheSize()) {
43 SkImageInfo info = SkImageInfo::MakeA8(displayInfo.w, displayInfo.h);
44 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(grContext, SkBudgeted::kYes, info);
45 surface->getCanvas()->drawColor(SK_AlphaTRANSPARENT);
46
47 grContext->flush();
48
49 surfaces.push_back(surface);
50 }
51
52 ASSERT_TRUE(1 < surfaces.size());
53
54 // attempt to trim all memory while we still hold strong refs
55 renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
56 ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes());
57
58 // free the surfaces
59 for (size_t i = 0; i < surfaces.size(); i++) {
60 ASSERT_TRUE(surfaces[i]->unique());
61 surfaces[i].reset();
62 }
63
64 // verify that we have enough purgeable bytes
65 const size_t purgeableBytes = grContext->getResourceCachePurgeableBytes();
66 ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() < purgeableBytes);
67
68 // UI hidden and make sure only some got purged
69 renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::UiHidden);
70 ASSERT_TRUE(0 < grContext->getResourceCachePurgeableBytes());
71 ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() > getCacheUsage(grContext));
72
73 // complete and make sure all get purged
74 renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
75 ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes());
76}