blob: 2b90bda87ecd2d297d84390d485c782ac1a9b3f8 [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>
Kevin Lubick8099b672023-01-09 14:48:45 +000024#include "include/gpu/GpuTypes.h" // from Skia
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -040025
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040026using namespace android;
27using namespace android::uirenderer;
28using namespace android::uirenderer::renderthread;
29
Adlai Hollerf8c434e2020-07-27 11:42:45 -040030static size_t getCacheUsage(GrDirectContext* grContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040031 size_t cacheUsage;
32 grContext->getResourceCacheUsage(nullptr, &cacheUsage);
33 return cacheUsage;
34}
35
Nolan Scobie81787fb2022-11-14 19:35:58 -050036// TOOD(258700630): fix this test and re-enable
37RENDERTHREAD_SKIA_PIPELINE_TEST(CacheManager, DISABLED_trimMemory) {
Alec Mouri22d753f2019-09-05 17:11:45 -070038 int32_t width = DeviceInfo::get()->getWidth();
39 int32_t height = DeviceInfo::get()->getHeight();
Adlai Hollerf8c434e2020-07-27 11:42:45 -040040 GrDirectContext* grContext = renderThread.getGrContext();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040041 ASSERT_TRUE(grContext != nullptr);
42
John Reck1bcacfd2017-11-03 10:12:19 -070043 // create pairs of offscreen render targets and images until we exceed the
44 // backgroundCacheSizeLimit
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040045 std::vector<sk_sp<SkSurface>> surfaces;
46
47 while (getCacheUsage(grContext) <= renderThread.cacheManager().getBackgroundCacheSize()) {
Alec Mouri22d753f2019-09-05 17:11:45 -070048 SkImageInfo info = SkImageInfo::MakeA8(width, height);
Kevin Lubick8099b672023-01-09 14:48:45 +000049 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(grContext, skgpu::Budgeted::kYes,
50 info);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040051 surface->getCanvas()->drawColor(SK_AlphaTRANSPARENT);
52
Greg Danielc7ad4082020-05-14 15:38:26 -040053 grContext->flushAndSubmit();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040054
55 surfaces.push_back(surface);
56 }
57
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -040058 // create an image and pin it so that we have something with a unique key in the cache
Alec Mouri22d753f2019-09-05 17:11:45 -070059 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(SkImageInfo::MakeA8(width, height));
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040060 sk_sp<SkImage> image = bitmap->makeImage();
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -040061 ASSERT_TRUE(SkImage_pinAsTexture(image.get(), grContext));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040062
63 // attempt to trim all memory while we still hold strong refs
John Reck5f66fb82022-09-23 17:49:23 -040064 renderThread.cacheManager().trimMemory(TrimLevel::COMPLETE);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040065 ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes());
66
67 // free the surfaces
68 for (size_t i = 0; i < surfaces.size(); i++) {
69 ASSERT_TRUE(surfaces[i]->unique());
70 surfaces[i].reset();
71 }
72
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -040073 // unpin the image which should add a unique purgeable key to the cache
74 SkImage_unpinAsTexture(image.get(), grContext);
75
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040076 // verify that we have enough purgeable bytes
77 const size_t purgeableBytes = grContext->getResourceCachePurgeableBytes();
78 ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() < purgeableBytes);
79
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -040080 // UI hidden and make sure only some got purged (unique should remain)
John Reck5f66fb82022-09-23 17:49:23 -040081 renderThread.cacheManager().trimMemory(TrimLevel::UI_HIDDEN);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040082 ASSERT_TRUE(0 < grContext->getResourceCachePurgeableBytes());
83 ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() > getCacheUsage(grContext));
84
85 // complete and make sure all get purged
John Reck5f66fb82022-09-23 17:49:23 -040086 renderThread.cacheManager().trimMemory(TrimLevel::COMPLETE);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040087 ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes());
88}