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