aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-12-06 16:45:24 +0800
committerYuqian Yang <crupest@crupest.life>2025-12-06 16:45:24 +0800
commit2ba85046b80d2763ed41f4aad44f1ae2f11c3a86 (patch)
treefe41a43853bf3607042dda7218ffbb8db9dfd196
parentad1ba6795040d569d3b4857fcd39cdb6a7ed1c21 (diff)
downloadcru-2ba85046b80d2763ed41f4aad44f1ae2f11c3a86.tar.gz
cru-2ba85046b80d2763ed41f4aad44f1ae2f11c3a86.tar.bz2
cru-2ba85046b80d2763ed41f4aad44f1ae2f11c3a86.zip
Use derive for opengl renderer painter.
-rw-r--r--include/cru/platform/graphics/cairo/CairoPainter.h4
-rw-r--r--src/platform/graphics/cairo/CairoPainter.cpp8
-rw-r--r--src/platform/gui/sdl/OpenGLRenderer.cpp20
-rw-r--r--src/ui/render/RenderObject.cpp12
4 files changed, 17 insertions, 27 deletions
diff --git a/include/cru/platform/graphics/cairo/CairoPainter.h b/include/cru/platform/graphics/cairo/CairoPainter.h
index 4688a7ff..1ffb7d47 100644
--- a/include/cru/platform/graphics/cairo/CairoPainter.h
+++ b/include/cru/platform/graphics/cairo/CairoPainter.h
@@ -4,7 +4,6 @@
#include <cru/base/Base.h>
#include <cru/platform/graphics/Painter.h>
-#include <functional>
#include <vector>
namespace cru::platform::graphics::cairo {
@@ -52,8 +51,6 @@ class CRU_PLATFORM_GRAPHICS_CAIRO_API CairoPainter : public CairoResource,
void EndDraw() override;
- void SetEndDrawCallback(std::function<void()> action);
-
private:
void CheckValidation();
@@ -61,7 +58,6 @@ class CRU_PLATFORM_GRAPHICS_CAIRO_API CairoPainter : public CairoResource,
cairo_t* cairo_;
bool auto_release_;
- std::function<void()> end_draw_callback_;
cairo_surface_t* cairo_surface_;
diff --git a/src/platform/graphics/cairo/CairoPainter.cpp b/src/platform/graphics/cairo/CairoPainter.cpp
index 30162506..c71d9359 100644
--- a/src/platform/graphics/cairo/CairoPainter.cpp
+++ b/src/platform/graphics/cairo/CairoPainter.cpp
@@ -1,5 +1,4 @@
#include "cru/platform/graphics/cairo/CairoPainter.h"
-#include "cru/base/log/Logger.h"
#include "cru/platform/graphics/cairo/Base.h"
#include "cru/platform/graphics/cairo/CairoBrush.h"
#include "cru/platform/graphics/cairo/CairoGeometry.h"
@@ -236,22 +235,15 @@ void CairoPainter::PopState() {
void CairoPainter::EndDraw() {
if (cairo_surface_ != nullptr) {
- CruLogDebug(kLogTag, "Flush cairo painter.");
cairo_surface_flush(cairo_surface_);
cairo_device_t* device = cairo_surface_get_device(cairo_surface_);
if (device) {
cairo_device_flush(device);
}
-
- if (end_draw_callback_) end_draw_callback_();
}
valid_ = false;
}
-void CairoPainter::SetEndDrawCallback(std::function<void()> action) {
- end_draw_callback_ = std::move(action);
-}
-
void CairoPainter::CheckValidation() {
if (!valid_) {
throw ReuseException("Painter already ended drawing.");
diff --git a/src/platform/gui/sdl/OpenGLRenderer.cpp b/src/platform/gui/sdl/OpenGLRenderer.cpp
index 78eb578e..dbd59db2 100644
--- a/src/platform/gui/sdl/OpenGLRenderer.cpp
+++ b/src/platform/gui/sdl/OpenGLRenderer.cpp
@@ -38,6 +38,21 @@ void main()
{
FragColor = texture(ourTexture, TexCoord);
})";
+
+class SdlOpenGLCairoRendererPainter : public graphics::cairo::CairoPainter {
+ public:
+ SdlOpenGLCairoRendererPainter(cairo_t* cairo, cairo_surface_t* surface,
+ SdlOpenGLRenderer* renderer)
+ : CairoPainter(nullptr, cairo, false, surface), renderer_(renderer) {}
+
+ void EndDraw() override {
+ CairoPainter::EndDraw();
+ renderer_->Present();
+ }
+
+ private:
+ SdlOpenGLRenderer* renderer_;
+};
} // namespace
SdlOpenGLRenderer::SdlOpenGLRenderer(SdlWindow* window, int width, int height) {
@@ -166,9 +181,8 @@ std::unique_ptr<graphics::IPainter> SdlOpenGLRenderer::BeginPaint() {
assert(cairo_surface_);
assert(gl_texture_);
- auto painter = std::make_unique<graphics::cairo::CairoPainter>(
- nullptr, cairo_, false, cairo_surface_);
- painter->SetEndDrawCallback([this] { Present(); });
+ auto painter = std::make_unique<SdlOpenGLCairoRendererPainter>(
+ cairo_, cairo_surface_, this);
return painter;
}
diff --git a/src/ui/render/RenderObject.cpp b/src/ui/render/RenderObject.cpp
index f6f081ec..f553ea6d 100644
--- a/src/ui/render/RenderObject.cpp
+++ b/src/ui/render/RenderObject.cpp
@@ -1,7 +1,6 @@
#include "cru/ui/render/RenderObject.h"
#include "cru/base/Base.h"
-#include "cru/base/log/Logger.h"
#include "cru/platform/GraphicsBase.h"
#include "cru/ui/controls/Control.h"
#include "cru/ui/controls/ControlHost.h"
@@ -82,9 +81,6 @@ void RenderObject::SetMaxSize(const MeasureSize& max_size) {
}
void RenderObject::Measure(const MeasureRequirement& requirement) {
- CruLogDebug(kLogTag, "{} Measure begins, requirement {}.",
- this->GetDebugPathInTree(), requirement);
-
if (layout_valid_ && requirement == last_measure_requirement_) {
return;
}
@@ -95,9 +91,6 @@ void RenderObject::Measure(const MeasureRequirement& requirement) {
}
last_measure_requirement_ = requirement;
-
- CruLogDebug(kLogTag, "{} Measure ends, result size: {}.",
- this->GetDebugPathInTree(), measure_result_size_);
}
void RenderObject::Layout(const Point& offset) {
@@ -105,9 +98,6 @@ void RenderObject::Layout(const Point& offset) {
}
void RenderObject::Layout(const Rect& rect) {
- CruLogDebug(kLogTag, "{} Layout begins, rect: {}.",
- this->GetDebugPathInTree(), rect);
-
offset_ = rect.GetLeftTop();
auto new_size = rect.GetSize();
if (size_ != new_size) {
@@ -117,8 +107,6 @@ void RenderObject::Layout(const Rect& rect) {
OnLayoutCore(rect);
layout_valid_ = true;
-
- CruLogDebug(kLogTag, "{} Layout ends.", this->GetDebugPathInTree());
}
Thickness RenderObject::GetTotalSpaceThickness() { return margin_ + padding_; }