|
| 1 | +#include <cputexturemanager.h> |
| 2 | +#include <texture.h> |
| 3 | +#include <qnanopainter.h> |
| 4 | + |
| 5 | +#include "../common.h" |
| 6 | + |
| 7 | +using namespace scratchcpprender; |
| 8 | + |
| 9 | +class CpuTextureManagerTest : public testing::Test |
| 10 | +{ |
| 11 | + public: |
| 12 | + void createContextAndSurface(QOpenGLContext *context, QOffscreenSurface *surface) |
| 13 | + { |
| 14 | + QSurfaceFormat surfaceFormat; |
| 15 | + surfaceFormat.setMajorVersion(4); |
| 16 | + surfaceFormat.setMinorVersion(3); |
| 17 | + |
| 18 | + context->setFormat(surfaceFormat); |
| 19 | + context->create(); |
| 20 | + ASSERT_TRUE(context->isValid()); |
| 21 | + |
| 22 | + surface->setFormat(surfaceFormat); |
| 23 | + surface->create(); |
| 24 | + ASSERT_TRUE(surface->isValid()); |
| 25 | + |
| 26 | + context->makeCurrent(surface); |
| 27 | + ASSERT_EQ(QOpenGLContext::currentContext(), context); |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +class ImagePainter |
| 32 | +{ |
| 33 | + public: |
| 34 | + ImagePainter(QNanoPainter *painter, const QString &fileName) |
| 35 | + { |
| 36 | + QOpenGLFramebufferObjectFormat format; |
| 37 | + format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); |
| 38 | + |
| 39 | + // Begin painting |
| 40 | + m_fbo = std::make_unique<QOpenGLFramebufferObject>(4, 6, format); |
| 41 | + m_fbo->bind(); |
| 42 | + painter->beginFrame(m_fbo->width(), m_fbo->height()); |
| 43 | + |
| 44 | + // Paint |
| 45 | + QNanoImage image = QNanoImage::fromCache(painter, fileName); |
| 46 | + painter->drawImage(image, 0, 0); |
| 47 | + painter->endFrame(); |
| 48 | + } |
| 49 | + |
| 50 | + ~ImagePainter() { m_fbo->release(); } |
| 51 | + |
| 52 | + QOpenGLFramebufferObject *fbo() const { return m_fbo.get(); }; |
| 53 | + |
| 54 | + private: |
| 55 | + std::unique_ptr<QOpenGLFramebufferObject> m_fbo; |
| 56 | +}; |
| 57 | + |
| 58 | +TEST_F(CpuTextureManagerTest, TextureDataAndHullPoints) |
| 59 | +{ |
| 60 | + static const GLubyte refData1[] = { |
| 61 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 255, 255, 255, 128, 128, 255, 0, 0, 0, 0, 0, 0, 128, 255, 0, 0, 0, 0, 87, 149, 87, 149, |
| 62 | + 0, 0, 0, 0, 128, 0, 128, 255, 128, 128, 255, 255, 128, 128, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 63 | + }; |
| 64 | + |
| 65 | + static const GLubyte refData2[] = { |
| 66 | + 0, 0, 57, 255, 10, 0, 50, 255, 43, 0, 35, 255, 60, 0, 28, 255, 0, 0, 55, 255, 39, 15, 73, 255, 137, 85, 133, 255, 207, 142, 182, 255, |
| 67 | + 10, 0, 50, 255, 23, 4, 50, 255, 4, 0, 7, 255, 204, 204, 196, 255, 11, 0, 35, 255, 59, 46, 76, 255, 135, 146, 140, 255, 99, 123, 99, 255, |
| 68 | + 4, 0, 12, 255, 1, 0, 7, 255, 0, 1, 0, 255, 0, 3, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255 |
| 69 | + }; |
| 70 | + |
| 71 | + static const std::vector<QPoint> refHullPoints1 = { { 1, 1 }, { 2, 1 }, { 3, 1 }, { 1, 2 }, { 3, 2 }, { 1, 3 }, { 2, 3 }, { 3, 3 } }; |
| 72 | + |
| 73 | + static const std::vector<QPoint> refHullPoints2 = { |
| 74 | + { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, { 0, 2 }, { 1, 2 }, { 2, 2 }, { 3, 2 }, |
| 75 | + { 0, 3 }, { 1, 3 }, { 2, 3 }, { 3, 3 }, { 0, 4 }, { 1, 4 }, { 2, 4 }, { 3, 4 }, { 0, 5 }, { 1, 5 }, { 2, 5 }, { 3, 5 } |
| 76 | + }; |
| 77 | + |
| 78 | + // Create OpenGL context |
| 79 | + QOpenGLContext context; |
| 80 | + QOffscreenSurface surface; |
| 81 | + createContextAndSurface(&context, &surface); |
| 82 | + |
| 83 | + // Paint images |
| 84 | + QNanoPainter painter; |
| 85 | + ImagePainter imgPainter1(&painter, "image.png"); |
| 86 | + ImagePainter imgPainter2(&painter, "image.jpg"); |
| 87 | + |
| 88 | + // Read texture data |
| 89 | + CpuTextureManager manager; |
| 90 | + |
| 91 | + for (int i = 0; i < 2; i++) { |
| 92 | + Texture texture1(imgPainter1.fbo()->texture(), imgPainter1.fbo()->size()); |
| 93 | + GLubyte *data = manager.getTextureData(texture1); |
| 94 | + ASSERT_EQ(memcmp(data, refData1, 96), 0); |
| 95 | + const auto &hullPoints1 = manager.getTextureConvexHullPoints(texture1); |
| 96 | + ASSERT_EQ(hullPoints1, refHullPoints1); |
| 97 | + |
| 98 | + Texture texture2(imgPainter2.fbo()->texture(), imgPainter2.fbo()->size()); |
| 99 | + data = manager.getTextureData(texture2); |
| 100 | + ASSERT_EQ(memcmp(data, refData2, 96), 0); |
| 101 | + const auto &hullPoints2 = manager.getTextureConvexHullPoints(texture2); |
| 102 | + ASSERT_EQ(hullPoints2, refHullPoints2); |
| 103 | + } |
| 104 | + |
| 105 | + // Cleanup |
| 106 | + context.doneCurrent(); |
| 107 | +} |
0 commit comments