1 /*
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 
23 #include <SkImageAndroid.h>
24 #include <include/gpu/ganesh/SkSurfaceGanesh.h>
25 #include "include/gpu/GpuTypes.h" // from Skia
26 
27 using namespace android;
28 using namespace android::uirenderer;
29 using namespace android::uirenderer::renderthread;
30 
getCacheUsage(GrDirectContext * grContext)31 static size_t getCacheUsage(GrDirectContext* grContext) {
32     size_t cacheUsage;
33     grContext->getResourceCacheUsage(nullptr, &cacheUsage);
34     return cacheUsage;
35 }
36 
37 // TOOD(258700630): fix this test and re-enable
RENDERTHREAD_TEST(CacheManager,DISABLED_trimMemory)38 RENDERTHREAD_TEST(CacheManager, DISABLED_trimMemory) {
39     int32_t width = DeviceInfo::get()->getWidth();
40     int32_t height = DeviceInfo::get()->getHeight();
41     GrDirectContext* grContext = renderThread.getGrContext();
42     ASSERT_TRUE(grContext != nullptr);
43 
44     // create pairs of offscreen render targets and images until we exceed the
45     // backgroundCacheSizeLimit
46     std::vector<sk_sp<SkSurface>> surfaces;
47 
48     while (getCacheUsage(grContext) <= renderThread.cacheManager().getBackgroundCacheSize()) {
49         SkImageInfo info = SkImageInfo::MakeA8(width, height);
50         sk_sp<SkSurface> surface = SkSurfaces::RenderTarget(grContext, skgpu::Budgeted::kYes,
51                                                             info);
52         surface->getCanvas()->drawColor(SK_AlphaTRANSPARENT);
53 
54         grContext->flushAndSubmit();
55 
56         surfaces.push_back(surface);
57     }
58 
59     // create an image and pin it so that we have something with a unique key in the cache
60     sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(SkImageInfo::MakeA8(width, height));
61     sk_sp<SkImage> image = bitmap->makeImage(); // calls skgpu::ganesh::PinAsTexture under the hood.
62     ASSERT_TRUE(skgpu::ganesh::PinAsTexture(grContext, image.get()));
63     // attempt to trim all memory while we still hold strong refs
64     renderThread.cacheManager().trimMemory(TrimLevel::COMPLETE);
65     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 
73     // unpin the image which should add a unique purgeable key to the cache
74     skgpu::ganesh::UnpinTexture(grContext, image.get());
75 
76     // verify that we have enough purgeable bytes
77     const size_t purgeableBytes = grContext->getResourceCachePurgeableBytes();
78     ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() < purgeableBytes);
79 
80     // UI hidden and make sure only some got purged (unique should remain)
81     renderThread.cacheManager().trimMemory(TrimLevel::UI_HIDDEN);
82     ASSERT_TRUE(0 < grContext->getResourceCachePurgeableBytes());
83     ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() > getCacheUsage(grContext));
84 
85     // complete and make sure all get purged
86     renderThread.cacheManager().trimMemory(TrimLevel::COMPLETE);
87     ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes());
88 }
89