2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <GpuMemoryTracker.h>
18 #include <gtest/gtest.h>
20 #include "renderthread/EglManager.h"
21 #include "renderthread/RenderThread.h"
22 #include "tests/common/TestUtils.h"
24 #include <utils/StrongPointer.h>
26 using namespace android;
27 using namespace android::uirenderer;
28 using namespace android::uirenderer::renderthread;
30 class TestGPUObject : public GpuMemoryTracker {
32 TestGPUObject() : GpuMemoryTracker(GpuObjectType::Texture) {}
34 void changeSize(int newSize) { notifySizeChanged(newSize); }
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
40 static void destroyEglContext() {
41 if (TestUtils::isRenderThreadRunning()) {
42 TestUtils::runOnRenderThread([](RenderThread& thread) { thread.destroyGlContext(); });
46 TEST(GpuMemoryTracker, sizeCheck) {
49 GpuMemoryTracker::onGpuContextCreated();
50 ASSERT_EQ(0, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
51 ASSERT_EQ(0, GpuMemoryTracker::getInstanceCount(GpuObjectType::Texture));
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));
62 ASSERT_EQ(0, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
63 ASSERT_EQ(0, GpuMemoryTracker::getInstanceCount(GpuObjectType::Texture));
64 GpuMemoryTracker::onGpuContextDestroyed();