Skip to content

Commit

Permalink
Add a DevicePool to tests to fix the testing failure caused by the nu…
Browse files Browse the repository at this point in the history
…mber limit of GPU context creation. (#22)
  • Loading branch information
domchen committed Nov 2, 2023
1 parent 3c84bf7 commit fe1aed0
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 22 deletions.
34 changes: 17 additions & 17 deletions test/src/CanvasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
namespace tgfx {

TGFX_TEST(CanvasTest, ColorMatrixFilter) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand All @@ -61,7 +61,7 @@ TGFX_TEST(CanvasTest, ColorMatrixFilter) {
}

TGFX_TEST(CanvasTest, Blur) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -133,7 +133,7 @@ TGFX_TEST(CanvasTest, Blur) {
}

TGFX_TEST(CanvasTest, DropShadow) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -174,7 +174,7 @@ TGFX_TEST(CanvasTest, DropShadow) {
}

TGFX_TEST(CanvasTest, clip) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -208,7 +208,7 @@ TGFX_TEST(CanvasTest, clip) {
}

TGFX_TEST(CanvasTest, TileMode) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand All @@ -228,7 +228,7 @@ TGFX_TEST(CanvasTest, TileMode) {
}

TGFX_TEST(CanvasTest, merge_draw_call_rect) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -265,7 +265,7 @@ TGFX_TEST(CanvasTest, merge_draw_call_rect) {
}

TGFX_TEST(CanvasTest, merge_draw_call_triangle) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -312,7 +312,7 @@ TGFX_TEST(CanvasTest, merge_draw_call_triangle) {
}

TGFX_TEST(CanvasTest, merge_draw_call_rrect) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -353,7 +353,7 @@ TGFX_TEST(CanvasTest, merge_draw_call_rrect) {
}

TGFX_TEST(CanvasTest, merge_draw_clear_op) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -451,7 +451,7 @@ TGFX_TEST(CanvasTest, textShape) {
}
height += lineHeight;

auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -479,7 +479,7 @@ TGFX_TEST(CanvasTest, textShape) {
}

TGFX_TEST(CanvasTest, filterMode) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand All @@ -499,7 +499,7 @@ TGFX_TEST(CanvasTest, filterMode) {
}

TGFX_TEST(CanvasTest, mipmap) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -548,7 +548,7 @@ TGFX_TEST(CanvasTest, mipmap) {
}

TGFX_TEST(CanvasTest, hardwareMipMap) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -579,7 +579,7 @@ TGFX_TEST(CanvasTest, hardwareMipMap) {
}

TGFX_TEST(CanvasTest, shape) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -613,7 +613,7 @@ TGFX_TEST(CanvasTest, shape) {
}

TGFX_TEST(CanvasTest, image) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -725,7 +725,7 @@ static GLTextureInfo CreateRectangleTexture(Context* context, int width, int hei
}

TGFX_TEST(CanvasTest, rectangleTextureAsBlendDst) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand All @@ -748,7 +748,7 @@ TGFX_TEST(CanvasTest, rectangleTextureAsBlendDst) {
}

TGFX_TEST(CanvasTest, NothingToDraw) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down
4 changes: 2 additions & 2 deletions test/src/ImageReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace tgfx {

TGFX_TEST(ImageReaderTest, updateMask) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down Expand Up @@ -64,7 +64,7 @@ TGFX_TEST(ImageReaderTest, updateMask) {
}

TGFX_TEST(ImageReaderTest, updateBitmap) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down
2 changes: 1 addition & 1 deletion test/src/MaskTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TGFX_TEST(MaskTest, Rasterize) {
auto maskBuffer = std::static_pointer_cast<PixelBuffer>(mask->makeBuffer());
EXPECT_TRUE(Baseline::Compare(maskBuffer, "MaskTest/rasterize_path"));

auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down
2 changes: 1 addition & 1 deletion test/src/ReadPixelsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ TGFX_TEST(ReadPixelsTest, Surface) {
bitmap.unlockPixels();
ASSERT_TRUE(result);

auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down
2 changes: 1 addition & 1 deletion test/src/SurfaceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace tgfx {

TGFX_TEST(SurfaceTest, ImageSnapshot) {
auto device = GLDevice::Make();
auto device = DevicePool::Make();
ASSERT_TRUE(device != nullptr);
auto context = device->lockContext();
ASSERT_TRUE(context != nullptr);
Expand Down
2 changes: 2 additions & 0 deletions test/src/base/TestEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "TestEnvironment.h"
#include "utils/Baseline.h"
#include "utils/DevicePool.h"

namespace tgfx {
void TestEnvironment::SetUp() {
Expand All @@ -26,6 +27,7 @@ void TestEnvironment::SetUp() {

void TestEnvironment::TearDown() {
Baseline::TearDown();
DevicePool::CleanAll();
}

} // namespace tgfx
44 changes: 44 additions & 0 deletions test/src/utils/DevicePool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "DevicePool.h"
#include <thread>

namespace tgfx {
static std::mutex threadCacheLocker = {};
static std::unordered_map<std::thread::id, std::shared_ptr<tgfx::GLDevice>> threadCacheMap = {};

std::shared_ptr<tgfx::GLDevice> DevicePool::Make() {
std::lock_guard<std::mutex> autoLock(threadCacheLocker);
auto threadID = std::this_thread::get_id();
auto result = threadCacheMap.find(threadID);
if (result != threadCacheMap.end()) {
return result->second;
}
auto device = tgfx::GLDevice::Make();
if (device != nullptr) {
threadCacheMap[threadID] = device;
}
return device;
}

void DevicePool::CleanAll() {
std::lock_guard<std::mutex> autoLock(threadCacheLocker);
threadCacheMap = {};
}
} // namespace tgfx
30 changes: 30 additions & 0 deletions test/src/utils/DevicePool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "tgfx/opengl/GLDevice.h"

namespace tgfx {
class DevicePool {
public:
static std::shared_ptr<tgfx::GLDevice> Make();

static void CleanAll();
};
} // namespace tgfx
1 change: 1 addition & 0 deletions test/src/utils/TestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "tgfx/core/Pixmap.h"
#include "tgfx/utils/Clock.h"
#include "utils/Baseline.h"
#include "utils/DevicePool.h"
#include "utils/ProjectPath.h"

namespace tgfx {
Expand Down

0 comments on commit fe1aed0

Please sign in to comment.