Skip to content

Commit 13dd9a6

Browse files
committed
Add removeTexture() method to CpuTextureManager
1 parent 47de551 commit 13dd9a6

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/cputexturemanager.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ const std::vector<QPoint> &CpuTextureManager::getTextureConvexHullPoints(const T
5151
return it->second;
5252
}
5353

54+
void CpuTextureManager::removeTexture(const Texture &texture)
55+
{
56+
if (!texture.isValid())
57+
return;
58+
59+
const GLuint handle = texture.handle();
60+
auto it = m_textureData.find(handle);
61+
62+
if (it != m_textureData.cend()) {
63+
delete it->second;
64+
m_textureData.erase(it);
65+
m_convexHullPoints.erase(handle);
66+
}
67+
}
68+
5469
bool CpuTextureManager::addTexture(const Texture &texture)
5570
{
5671
if (!texture.isValid())

src/cputexturemanager.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class CpuTextureManager
2020
GLubyte *getTextureData(const Texture &texture);
2121
const std::vector<QPoint> &getTextureConvexHullPoints(const Texture &texture);
2222

23+
void removeTexture(const Texture &texture);
24+
2325
private:
2426
bool addTexture(const Texture &texture);
2527

test/texture/cputexturemanager_test.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,24 @@ class ImagePainter
3636
QOpenGLFramebufferObjectFormat format;
3737
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
3838

39-
// Begin painting
4039
m_fbo = std::make_unique<QOpenGLFramebufferObject>(4, 6, format);
40+
paint(painter, fileName);
41+
}
42+
43+
void paint(QNanoPainter *painter, const QString &fileName)
44+
{
45+
// Begin painting
4146
m_fbo->bind();
4247
painter->beginFrame(m_fbo->width(), m_fbo->height());
48+
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
49+
glClear(GL_COLOR_BUFFER_BIT);
4350

4451
// Paint
4552
QNanoImage image = QNanoImage::fromCache(painter, fileName);
4653
painter->drawImage(image, 0, 0);
4754
painter->endFrame();
55+
56+
m_fbo->release();
4857
}
4958

5059
~ImagePainter() { m_fbo->release(); }
@@ -102,6 +111,21 @@ TEST_F(CpuTextureManagerTest, TextureDataAndHullPoints)
102111
ASSERT_EQ(hullPoints2, refHullPoints2);
103112
}
104113

114+
// Test removeTexture()
115+
imgPainter1.paint(&painter, "image.jpg");
116+
Texture texture(imgPainter1.fbo()->texture(), imgPainter1.fbo()->size());
117+
GLubyte *data = manager.getTextureData(texture);
118+
ASSERT_EQ(memcmp(data, refData1, 96), 0);
119+
const auto &hullPoints1 = manager.getTextureConvexHullPoints(texture);
120+
ASSERT_EQ(hullPoints1, refHullPoints1);
121+
122+
imgPainter1.fbo()->toImage().save("/home/adazem009/test.png");
123+
manager.removeTexture(texture);
124+
data = manager.getTextureData(texture);
125+
ASSERT_EQ(memcmp(data, refData2, 96), 0);
126+
const auto &hullPoints2 = manager.getTextureConvexHullPoints(texture);
127+
ASSERT_EQ(hullPoints2, refHullPoints2);
128+
105129
// Cleanup
106130
context.doneCurrent();
107131
}

0 commit comments

Comments
 (0)