aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-12-05 21:04:36 +0800
committerYuqian Yang <crupest@crupest.life>2025-12-05 21:04:36 +0800
commitad1ba6795040d569d3b4857fcd39cdb6a7ed1c21 (patch)
treeee0ee976bddc9e27db0da75ad2a78ff778206fcd
parentf5c801de04f423269d9cefa3d725b52b87c7a0b9 (diff)
downloadcru-ad1ba6795040d569d3b4857fcd39cdb6a7ed1c21.tar.gz
cru-ad1ba6795040d569d3b4857fcd39cdb6a7ed1c21.tar.bz2
cru-ad1ba6795040d569d3b4857fcd39cdb6a7ed1c21.zip
Add Measure time log. Clean up OpenGL renderer.
-rw-r--r--include/cru/base/log/Logger.h4
-rw-r--r--src/base/log/Logger.cpp13
-rw-r--r--src/platform/gui/sdl/OpenGLRenderer.cpp11
3 files changed, 20 insertions, 8 deletions
diff --git a/include/cru/base/log/Logger.h b/include/cru/base/log/Logger.h
index d98ee3e3..6290378b 100644
--- a/include/cru/base/log/Logger.h
+++ b/include/cru/base/log/Logger.h
@@ -1,5 +1,6 @@
#pragma once
#include "../Base.h"
+#include "../Guard.h"
#include <condition_variable>
#include <format>
@@ -86,6 +87,9 @@ class CRU_BASE_API Logger : public Object, public virtual ILogger {
std::mutex target_list_mutex_;
std::vector<std::unique_ptr<ILogTarget>> target_list_;
};
+
+CRU_BASE_API Guard MeasureTimeAndLog(std::string_view tag,
+ std::string_view name);
} // namespace cru::log
#define CRU_DEFINE_LOG_FUNC(level) \
diff --git a/src/base/log/Logger.cpp b/src/base/log/Logger.cpp
index d75e006b..58fa6220 100644
--- a/src/base/log/Logger.cpp
+++ b/src/base/log/Logger.cpp
@@ -3,6 +3,7 @@
#include "cru/base/log/StdioLogTarget.h"
#include <algorithm>
+#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <ctime>
@@ -167,4 +168,16 @@ void Logger::LogThreadRun() {
if (stop) return;
}
}
+
+Guard MeasureTimeAndLog(std::string_view tag, std::string_view name) {
+ CruLogDebug(std::string(tag), "Start measure {}.", name);
+ auto start = std::chrono::high_resolution_clock::now();
+
+ return Guard([tag = std::string(tag), name = std::string(name), start] {
+ auto end = std::chrono::high_resolution_clock::now();
+ auto duration =
+ std::chrono::duration_cast<std::chrono::microseconds>(end - start);
+ CruLogDebug(tag, "End measure {}, time {} us.", name, duration.count());
+ });
+}
} // namespace cru::log
diff --git a/src/platform/gui/sdl/OpenGLRenderer.cpp b/src/platform/gui/sdl/OpenGLRenderer.cpp
index 07b58147..78eb578e 100644
--- a/src/platform/gui/sdl/OpenGLRenderer.cpp
+++ b/src/platform/gui/sdl/OpenGLRenderer.cpp
@@ -93,6 +93,8 @@ SdlOpenGLRenderer::SdlOpenGLRenderer(SdlWindow* window, int width, int height) {
reinterpret_cast<void*>(3 * sizeof(float)));
glad_gl_context_.EnableVertexAttribArray(1);
+ glad_gl_context_.UseProgram(gl_shader_program_);
+
Resize(width, height);
}
@@ -174,19 +176,12 @@ void SdlOpenGLRenderer::Present() {
assert(cairo_surface_);
assert(gl_texture_);
- auto _ = MakeContextCurrent();
+ auto context_guard = MakeContextCurrent();
auto data = cairo_image_surface_get_data(cairo_surface_);
glad_gl_context_.TexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, width_, height_, 0,
GL_BGRA, GL_UNSIGNED_BYTE, data);
- glad_gl_context_.UseProgram(gl_shader_program_);
- glad_gl_context_.ActiveTexture(GL_TEXTURE0);
- glad_gl_context_.BindTexture(GL_TEXTURE_2D, gl_texture_);
- glad_gl_context_.BindVertexArray(gl_vertex_array_);
- glad_gl_context_.BindBuffer(GL_ARRAY_BUFFER, gl_vertex_buffer_);
- glad_gl_context_.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_element_buffer_);
-
glad_gl_context_.DrawElements(
GL_TRIANGLES, sizeof(kIndices) / sizeof(*kIndices), GL_UNSIGNED_INT, 0);
CheckSdlReturn(SDL_GL_SwapWindow(sdl_window_));