blob: 08b967964c59b4c49657c7764248f67b58c7a3a3 [file] [log] [blame]
John Reck38e0c322015-11-10 12:19:17 -08001/*
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
John Reck38e0c322015-11-10 12:19:17 -080017#include <GpuMemoryTracker.h>
John Reck1bcacfd2017-11-03 10:12:19 -070018#include <gtest/gtest.h>
John Reck38e0c322015-11-10 12:19:17 -080019
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
John Reck1bcacfd2017-11-03 10:12:19 -070034 void changeSize(int newSize) { notifySizeChanged(newSize); }
John Reck38e0c322015-11-10 12:19:17 -080035};
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()) {
John Reck1e510712018-04-23 08:15:03 -070042 TestUtils::runOnRenderThread([](RenderThread& thread) { thread.destroyGlContext(); });
John Reck38e0c322015-11-10 12:19:17 -080043 }
44}
45
46TEST(GpuMemoryTracker, sizeCheck) {
47 destroyEglContext();
48
Greg Daniel45ec62b2017-01-04 14:27:00 -050049 GpuMemoryTracker::onGpuContextCreated();
John Reck38e0c322015-11-10 12:19:17 -080050 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));
Greg Daniel45ec62b2017-01-04 14:27:00 -050064 GpuMemoryTracker::onGpuContextDestroyed();
John Reck38e0c322015-11-10 12:19:17 -080065}