1/*
2 * Copyright (C) 2016 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
18#include <gtest/gtest.h>
19#include <GpuMemoryTracker.h>
20
21#include "renderthread/EglManager.h"
22#include "renderthread/RenderThread.h"
23#include "tests/common/TestUtils.h"
24
25#include <utils/StrongPointer.h>
26
27using namespace android;
28using namespace android::uirenderer;
29using namespace android::uirenderer::renderthread;
30
31class TestGPUObject : public GpuMemoryTracker {
32public:
33    TestGPUObject() : GpuMemoryTracker(GpuObjectType::Texture) {}
34
35    void changeSize(int newSize) {
36        notifySizeChanged(newSize);
37    }
38};
39
40// Other tests may have created a renderthread and EGL context.
41// This will destroy the EGLContext on RenderThread if it exists so that the
42// current thread can spoof being a GPU thread
43static void destroyEglContext() {
44    if (TestUtils::isRenderThreadRunning()) {
45        TestUtils::runOnRenderThread([](RenderThread& thread) {
46            thread.eglManager().destroy();
47        });
48    }
49}
50
51TEST(GpuMemoryTracker, sizeCheck) {
52    destroyEglContext();
53
54    GpuMemoryTracker::onGLContextCreated();
55    ASSERT_EQ(0, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
56    ASSERT_EQ(0, GpuMemoryTracker::getInstanceCount(GpuObjectType::Texture));
57    {
58        TestGPUObject myObj;
59        ASSERT_EQ(1, GpuMemoryTracker::getInstanceCount(GpuObjectType::Texture));
60        myObj.changeSize(500);
61        ASSERT_EQ(500, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
62        myObj.changeSize(1000);
63        ASSERT_EQ(1000, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
64        myObj.changeSize(300);
65        ASSERT_EQ(300, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
66    }
67    ASSERT_EQ(0, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
68    ASSERT_EQ(0, GpuMemoryTracker::getInstanceCount(GpuObjectType::Texture));
69    GpuMemoryTracker::onGLContextDestroyed();
70}
71