File: CacheManagerTests.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 326,092 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,181; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 117; sed: 19
file content (86 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include "renderthread/CacheManager.h"
#include "renderthread/EglManager.h"
#include "tests/common/TestUtils.h"

#include <SkImagePriv.h>

using namespace android;
using namespace android::uirenderer;
using namespace android::uirenderer::renderthread;

static size_t getCacheUsage(GrDirectContext* grContext) {
    size_t cacheUsage;
    grContext->getResourceCacheUsage(nullptr, &cacheUsage);
    return cacheUsage;
}

// TOOD(258700630): fix this test and re-enable
RENDERTHREAD_SKIA_PIPELINE_TEST(CacheManager, DISABLED_trimMemory) {
    int32_t width = DeviceInfo::get()->getWidth();
    int32_t height = DeviceInfo::get()->getHeight();
    GrDirectContext* grContext = renderThread.getGrContext();
    ASSERT_TRUE(grContext != nullptr);

    // create pairs of offscreen render targets and images until we exceed the
    // backgroundCacheSizeLimit
    std::vector<sk_sp<SkSurface>> surfaces;

    while (getCacheUsage(grContext) <= renderThread.cacheManager().getBackgroundCacheSize()) {
        SkImageInfo info = SkImageInfo::MakeA8(width, height);
        sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(grContext, SkBudgeted::kYes, info);
        surface->getCanvas()->drawColor(SK_AlphaTRANSPARENT);

        grContext->flushAndSubmit();

        surfaces.push_back(surface);
    }

    // create an image and pin it so that we have something with a unique key in the cache
    sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(SkImageInfo::MakeA8(width, height));
    sk_sp<SkImage> image = bitmap->makeImage();
    ASSERT_TRUE(SkImage_pinAsTexture(image.get(), grContext));

    // attempt to trim all memory while we still hold strong refs
    renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
    ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes());

    // free the surfaces
    for (size_t i = 0; i < surfaces.size(); i++) {
        ASSERT_TRUE(surfaces[i]->unique());
        surfaces[i].reset();
    }

    // unpin the image which should add a unique purgeable key to the cache
    SkImage_unpinAsTexture(image.get(), grContext);

    // verify that we have enough purgeable bytes
    const size_t purgeableBytes = grContext->getResourceCachePurgeableBytes();
    ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() < purgeableBytes);

    // UI hidden and make sure only some got purged (unique should remain)
    renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::UiHidden);
    ASSERT_TRUE(0 < grContext->getResourceCachePurgeableBytes());
    ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() > getCacheUsage(grContext));

    // complete and make sure all get purged
    renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
    ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes());
}