summaryrefslogtreecommitdiff
path: root/Tests/LibGL/TestRender.cpp
diff options
context:
space:
mode:
authorJelle Raaijmakers <jelle@gmta.nl>2022-04-15 01:08:15 +0200
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2022-04-17 09:58:29 +0430
commit8cfabbcd9330fad62cace2a6c0f3a15d9b39cfe3 (patch)
tree4faef42a9d05e9953664ef8f71021c0868500ebf /Tests/LibGL/TestRender.cpp
parent757f506fda6e5f2fd528f75185b6a2cd7524a23d (diff)
downloadserenity-8cfabbcd9330fad62cace2a6c0f3a15d9b39cfe3.zip
Tests: Implement reference image testing for LibGL
Each LibGL test can now be tested against a reference QOI image. Initially, these images can be generated by setting `SAVE_OUTPUT` to `true`, which will save a bunch of QOI images to `/home/anon`.
Diffstat (limited to 'Tests/LibGL/TestRender.cpp')
-rw-r--r--Tests/LibGL/TestRender.cpp101
1 files changed, 71 insertions, 30 deletions
diff --git a/Tests/LibGL/TestRender.cpp b/Tests/LibGL/TestRender.cpp
index a6bd64d982..7c67097b07 100644
--- a/Tests/LibGL/TestRender.cpp
+++ b/Tests/LibGL/TestRender.cpp
@@ -1,60 +1,101 @@
/*
- * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>.
+ * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
+ * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibTest/TestCase.h>
-
-#include <AK/Debug.h>
-#include <AK/Format.h>
+#include <AK/LexicalPath.h>
+#include <AK/String.h>
+#include <LibCore/FileStream.h>
#include <LibGL/GL/gl.h>
#include <LibGL/GLContext.h>
-#include <LibGfx/BMPWriter.h>
#include <LibGfx/Bitmap.h>
-#include <LibGfx/Font/FontDatabase.h>
-#include <fcntl.h>
-#include <unistd.h>
+#include <LibGfx/QOIWriter.h>
+#include <LibTest/TestCase.h>
-#define RENDER_WIDTH 16
-#define RENDER_HEIGHT 16
+#ifdef __serenity__
+# define REFERENCE_IMAGE_DIR "/usr/Tests/LibGL/reference-images"
+#else
+# define REFERENCE_IMAGE_DIR "reference-images"
+#endif
+#define SAVE_OUTPUT false
-TEST_CASE(simple_triangle)
+static NonnullOwnPtr<GL::GLContext> create_testing_context(int width, int height)
{
- auto bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { RENDER_WIDTH, RENDER_HEIGHT }));
+ auto bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { width, height }));
auto context = GL::create_context(*bitmap);
-
GL::make_context_current(context);
+ return context;
+}
+
+static void expect_bitmap_equals_reference(Gfx::Bitmap const& bitmap, StringView test_name)
+{
+ auto reference_filename = String::formatted("{}.qoi", test_name);
+
+ if constexpr (SAVE_OUTPUT) {
+ auto target_path = LexicalPath("/home/anon").append(reference_filename);
+ auto qoi_buffer = Gfx::QOIWriter::encode(bitmap);
+ auto qoi_output_stream = MUST(Core::OutputFileStream::open(target_path.string()));
+ auto number_of_bytes_written = qoi_output_stream.write(qoi_buffer);
+ qoi_output_stream.close();
+ EXPECT_EQ(number_of_bytes_written, qoi_buffer.size());
+ }
+
+ auto reference_image_path = String::formatted(REFERENCE_IMAGE_DIR "/{}", reference_filename);
+ auto reference_bitmap = MUST(Gfx::Bitmap::try_load_from_file(reference_image_path));
+ EXPECT_EQ(reference_bitmap->visually_equals(bitmap), true);
+}
+
+TEST_CASE(0001_simple_triangle)
+{
+ auto context = create_testing_context(64, 64);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
- glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClearDepth(1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
- glColor4f(1, 1, 1, 1);
+ glColor3f(1, 1, 1);
glVertex2f(0, 1);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glEnd();
+ EXPECT_EQ(glGetError(), 0u);
+
context->present();
+ expect_bitmap_equals_reference(context->frontbuffer(), "0001_simple_triangle");
+}
+
+TEST_CASE(0002_quad_color_interpolation)
+{
+ auto context = create_testing_context(64, 64);
+
+ glFrontFace(GL_CCW);
+ glCullFace(GL_BACK);
+ glEnable(GL_CULL_FACE);
+
+ glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ glBegin(GL_QUADS);
+
+ glColor3f(1, 0, 0);
+ glVertex2i(-1, -1);
+ glColor3f(0, 1, 0);
+ glVertex2i(1, -1);
+ glColor3f(0, 0, 1);
+ glVertex2i(1, 1);
+ glColor3f(1, 0, 1);
+ glVertex2i(-1, 1);
+ glEnd();
EXPECT_EQ(glGetError(), 0u);
- // FIXME: Verify that the image is indeed correct
-
- if constexpr (GL_DEBUG) {
- // output the image to manually verify that the output is correct
- Gfx::BMPWriter writer {};
- auto buffer = writer.dump(bitmap);
- int fd = open("./picture.bmp", O_CREAT | O_WRONLY, 0755);
- EXPECT(fd > 0);
- ssize_t nwritten = write(fd, buffer.data(), buffer.size());
- EXPECT_EQ((size_t)nwritten, buffer.size());
- close(fd);
- }
+
+ context->present();
+ expect_bitmap_equals_reference(context->frontbuffer(), "0002_quad_color_interpolation");
}