blob: c235715073f5831240a4ab898fbfd59bebde0830 [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
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -040023#include <SkImagePriv.h>
24
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040025using namespace android;
26using namespace android::uirenderer;
27using namespace android::uirenderer::renderthread;
28
29static size_t getCacheUsage(GrContext* grContext) {
30 size_t cacheUsage;
31 grContext->getResourceCacheUsage(nullptr, &cacheUsage);
32 return cacheUsage;
33}
34
35RENDERTHREAD_SKIA_PIPELINE_TEST(CacheManager, trimMemory) {
36 DisplayInfo displayInfo = renderThread.mainDisplayInfo();
37 GrContext* grContext = renderThread.getGrContext();
38 ASSERT_TRUE(grContext != nullptr);
39
John Reck1bcacfd2017-11-03 10:12:19 -070040 // create pairs of offscreen render targets and images until we exceed the
41 // backgroundCacheSizeLimit
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040042 std::vector<sk_sp<SkSurface>> surfaces;
43
44 while (getCacheUsage(grContext) <= renderThread.cacheManager().getBackgroundCacheSize()) {
45 SkImageInfo info = SkImageInfo::MakeA8(displayInfo.w, displayInfo.h);
46 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(grContext, SkBudgeted::kYes, info);
47 surface->getCanvas()->drawColor(SK_AlphaTRANSPARENT);
48
49 grContext->flush();
50
51 surfaces.push_back(surface);
52 }
53
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -040054 // create an image and pin it so that we have something with a unique key in the cache
55 sk_sp<Bitmap> bitmap =
56 Bitmap::allocateHeapBitmap(SkImageInfo::MakeA8(displayInfo.w, displayInfo.h));
57 sk_sp<SkColorFilter> filter;
58 sk_sp<SkImage> image = bitmap->makeImage(&filter);
59 ASSERT_TRUE(SkImage_pinAsTexture(image.get(), grContext));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040060
61 // attempt to trim all memory while we still hold strong refs
62 renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
63 ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes());
64
65 // free the surfaces
66 for (size_t i = 0; i < surfaces.size(); i++) {
67 ASSERT_TRUE(surfaces[i]->unique());
68 surfaces[i].reset();
69 }
70
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -040071 // unpin the image which should add a unique purgeable key to the cache
72 SkImage_unpinAsTexture(image.get(), grContext);
73
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040074 // verify that we have enough purgeable bytes
75 const size_t purgeableBytes = grContext->getResourceCachePurgeableBytes();
76 ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() < purgeableBytes);
77
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -040078 // UI hidden and make sure only some got purged (unique should remain)
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040079 renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::UiHidden);
80 ASSERT_TRUE(0 < grContext->getResourceCachePurgeableBytes());
81 ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() > getCacheUsage(grContext));
82
83 // complete and make sure all get purged
84 renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
85 ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes());
86}