GpuMemoryTrackerTests.cpp revision 38e0c32852e3b9d8ca4a9d3791577f52536419cb
138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck/*
238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck * Copyright (C) 2016 The Android Open Source Project
338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *
438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck * Licensed under the Apache License, Version 2.0 (the "License");
538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck * you may not use this file except in compliance with the License.
638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck * You may obtain a copy of the License at
738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *
838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *      http://www.apache.org/licenses/LICENSE-2.0
938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *
1038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck * Unless required by applicable law or agreed to in writing, software
1138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck * distributed under the License is distributed on an "AS IS" BASIS,
1238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck * See the License for the specific language governing permissions and
1438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck * limitations under the License.
1538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck */
1638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
1738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
1838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include <gtest/gtest.h>
1938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include <GpuMemoryTracker.h>
2038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
2138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include "renderthread/EglManager.h"
2238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include "renderthread/RenderThread.h"
2338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include "tests/common/TestUtils.h"
2438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
2538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include <utils/StrongPointer.h>
2638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
2738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckusing namespace android;
2838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckusing namespace android::uirenderer;
2938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckusing namespace android::uirenderer::renderthread;
3038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
3138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckclass TestGPUObject : public GpuMemoryTracker {
3238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckpublic:
3338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    TestGPUObject() : GpuMemoryTracker(GpuObjectType::Texture) {}
3438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
3538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    void changeSize(int newSize) {
3638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        notifySizeChanged(newSize);
3738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    }
3838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck};
3938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
4038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck// Other tests may have created a renderthread and EGL context.
4138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck// This will destroy the EGLContext on RenderThread if it exists so that the
4238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck// current thread can spoof being a GPU thread
4338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckstatic void destroyEglContext() {
4438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    if (TestUtils::isRenderThreadRunning()) {
4538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        TestUtils::runOnRenderThread([](RenderThread& thread) {
4638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            thread.eglManager().destroy();
4738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        });
4838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    }
4938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck}
5038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
5138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn ReckTEST(GpuMemoryTracker, sizeCheck) {
5238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    destroyEglContext();
5338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
5438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    GpuMemoryTracker::onGLContextCreated();
5538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    ASSERT_EQ(0, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
5638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    ASSERT_EQ(0, GpuMemoryTracker::getInstanceCount(GpuObjectType::Texture));
5738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    {
5838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        TestGPUObject myObj;
5938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        ASSERT_EQ(1, GpuMemoryTracker::getInstanceCount(GpuObjectType::Texture));
6038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        myObj.changeSize(500);
6138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        ASSERT_EQ(500, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
6238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        myObj.changeSize(1000);
6338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        ASSERT_EQ(1000, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
6438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        myObj.changeSize(300);
6538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        ASSERT_EQ(300, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
6638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    }
6738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    ASSERT_EQ(0, GpuMemoryTracker::getTotalSize(GpuObjectType::Texture));
6838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    ASSERT_EQ(0, GpuMemoryTracker::getInstanceCount(GpuObjectType::Texture));
6938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    GpuMemoryTracker::onGLContextDestroyed();
7038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck}
71