Skip to content

Commit

Permalink
cairo: fixed cairo for opengl 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
rxdu committed Nov 14, 2024
1 parent 3a1feb4 commit a36bcf9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/imview/include/imview/component/cairo_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define CAIRO_CONTEXT_HPP

#include <cairo.h>
#include <GL/gl.h>

#include <cstdint>
#include <stack>
Expand Down Expand Up @@ -51,7 +50,7 @@ class CairoContext {
void PopScale();

// GL-related functions can only be called after a GL context has been created
GLuint RenderToGlTexture();
uint32_t RenderToGlTexture();

// get object of Cairo context and surface
cairo_t* GetCairoObject() { return cr_; }
Expand All @@ -67,7 +66,7 @@ class CairoContext {

cairo_surface_t* surface_ = nullptr;
cairo_t* cr_ = nullptr;
GLuint image_texture_;
uint32_t image_texture_;

float aspect_ratio_ = 1.0;

Expand Down
27 changes: 25 additions & 2 deletions src/imview/src/component/cairo_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "imview/component/cairo_context.hpp"

#include <glad/glad.h>

#include <iostream>

namespace quickviz {
Expand Down Expand Up @@ -89,13 +91,34 @@ void CairoContext::PopScale() {
scaler_stack_.pop();
}

GLuint CairoContext::RenderToGlTexture() {
uint32_t CairoContext::RenderToGlTexture() {
// bind texture
glBindTexture(GL_TEXTURE_2D, image_texture_);

// convert surface to OpenGL texture
unsigned char* data = cairo_image_surface_get_data(surface_);
glTexImage2D(GL_TEXTURE_2D, 0, 4, width_, height_, 0, GL_BGRA,
cairo_format_t format = cairo_image_surface_get_format(surface_);
GLenum gl_format;
if (format == CAIRO_FORMAT_ARGB32) {
gl_format = GL_RGBA;
for (int i = 0; i < width_ * height_; ++i) {
unsigned char* pixel = &data[i * 4];
unsigned char a = pixel[3];
unsigned char r = pixel[2];
unsigned char g = pixel[1];
unsigned char b = pixel[0];
pixel[0] = r;
pixel[1] = g;
pixel[2] = b;
pixel[3] = a;
}
} else if (format == CAIRO_FORMAT_RGB24) {
gl_format = GL_RGB;
} else {
throw std::runtime_error(
"[ERROR] render_to_gl_texture() - Unsupported Cairo format\n");
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, gl_format,
GL_UNSIGNED_BYTE, data);

// unbind texture
Expand Down
2 changes: 1 addition & 1 deletion src/imview/src/widget/cairo_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void CairoWidget::Render(const ImVec2& uv0, const ImVec2& uv1,
draw_func(ctx_->GetCairoObject(), ctx_->GetAspectRatio());

// render cairo content to OpenGL texture
GLuint image = ctx_->RenderToGlTexture();
uint32_t image = ctx_->RenderToGlTexture();
ImGui::Image((void*)(intptr_t)image, ImGui::GetContentRegionAvail(), uv0, uv1,
tint_col, border_col);
}
Expand Down

0 comments on commit a36bcf9

Please sign in to comment.