Skip to contentSkip to conversation

Add support for text rendering

RFC PATCH In review Yordan Karadzhov (VMware) 3 participants 8 messages Jul 3, 2019 – Jul 5, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover Add support for text rendering

Implemented using the single-file public domain library "stb_truetype":
https://github.com/nothings/stb/blob/master/stb_truetype.h

PS: Patch 1/3 is a simple fix suggested by Slavomir which strictly speaking is independent from the other two patches.

v2 changes:
- 2 new patches (patches 4/5 and 5/5).

- Added a procedure for finding a font file (patch 2/5).

- Addressed the comments made by Slavomir in his review of the previous version.

Yordan Karadzhov (VMware) (5):
kernel-shark: Simplify the drawing of the Triangle Fan
kernel-shark: Add support for drawing text
kernel-shark: Add text rendering to the plotting example
kernel-shark: Add hello_kernel plugin
kernel-shark: Drop Freeglut as a compulsory dependency

 kernel-shark/build/deff.h.cmake      |    3 +
 kernel-shark/doc/dox_config          |    1 +
 kernel-shark/examples/CMakeLists.txt |   14 +-
 kernel-shark/examples/dataplot.cpp   |   11 +
 kernel-shark/examples/hello_kernel.c |   67 +
 kernel-shark/src/CMakeLists.txt      |    4 +-
 kernel-shark/src/KsGLWidget.cpp      |    4 -
 kernel-shark/src/KsPlotTools.cpp     |    4 -
 kernel-shark/src/libkshark-plot.c    |  239 +-
 kernel-shark/src/libkshark-plot.h    |   46 +
 kernel-shark/src/libkshark-plugin.h  |    3 +
 kernel-shark/src/stb_truetype.h      | 4882 ++++++++++++++++++++++++++
 12 files changed, 5243 insertions(+), 35 deletions(-)
 create mode 100644 kernel-shark/examples/hello_kernel.c
 create mode 100644 kernel-shark/src/stb_truetype.h
Signature · 1 line
2.20.1
#1 lore raw

RFC PATCH v2 1/5 kernel-shark: Simplify the drawing of the Triangle Fan

The central vertex of the Triangle Fan doesn't need to be an internal point. The first point of the polygon can be used instead.

---
 kernel-shark/src/libkshark-plot.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
kernel-shark/src/libkshark-plot.c +1 -10
index 17d3b90..cdd0ef8 100644
@@ -162,18 +162,9 @@ void ksplot_draw_polygon(const struct ksplot_point *points,
 		return;
 	}
 
-	/* Obtain a point inside the surface of the polygon. */
-	struct ksplot_point in_point;
-	in_point.x = (points[0].x + points[2].x) / 2;
-	in_point.y = (points[0].y + points[2].y) / 2;
-
-	/*
-	 * Draw a Triangle Fan using the internal point as a central
-	 * vertex.
-	 */
+	/* Draw a Triangle Fan. */
 	glBegin(GL_TRIANGLE_FAN);
 	glColor3ub(col->red, col->green, col->blue);
-	glVertex2i(in_point.x, in_point.y);
 	for (size_t i = 0; i < n_points; ++i)
 		glVertex2i(points[i].x, points[i].y);
 
Signature · 1 line
2.20.1
#2 lore raw

RFC PATCH v2 2/5 kernel-shark: Add support for drawing text

In reply to Yordan Karadzhov (VMware)

OpenGL doesn't provide support for text rendering. Here we add capability of printing text to the OpenGL scene by using the single-file public domain library "stb_truetype":
https://github.com/nothings/stb/blob/master/stb_truetype.h

---
 kernel-shark/doc/dox_config       |    1 +
 kernel-shark/src/libkshark-plot.c |  211 +-
 kernel-shark/src/libkshark-plot.h |   46 +
 kernel-shark/src/stb_truetype.h   | 4882 +++++++++++++++++++++++++++++
 4 files changed, 5137 insertions(+), 3 deletions(-)
 create mode 100644 kernel-shark/src/stb_truetype.h
kernel-shark/doc/dox_config +1 -0
index c6c53df..d9431bd 100644
@@ -13,3 +13,4 @@ CASE_SENSE_NAMES       = YES
 SORT_MEMBER_DOCS       = NO
 STRICT_PROTO_MATCHING  = YES
 DOT_MULTI_TARGETS      = YES
+EXCLUDE                = ../src/stb_truetype.h
kernel-shark/src/libkshark-plot.c +208 -3

224 lines not on this page · raw message

kernel-shark/src/libkshark-plot.h +46 -0

58 lines not on this page · raw message

kernel-shark/src/stb_truetype.h new file +4882 -0

2,714 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.20.1

Part of the diff is not shown here. View the raw message.

#3 lore raw

RFC PATCH v2 3/5 kernel-shark: Add text rendering to the plotting example

In reply to Yordan Karadzhov (VMware)

run:
kernel-shark/bin/dplot -s

And if the font file is loaded successfully you will see "Hello Kernel!" rendered.

---
 kernel-shark/examples/dataplot.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)
kernel-shark/examples/dataplot.cpp +11 -0
index 94841e7..11b4fd9 100644
@@ -44,6 +44,12 @@ void usage(const char *prog)
 /* An example function drawing something. */
 void drawShapes()
 {
+	char *font_file = ksplot_find_font_file("FreeMono", "FreeMonoBold");
+	struct ksplot_font font;
+
+	ksplot_init_font(&font, 40, font_file);
+	free(font_file);
+
 	/* Clear the screen. */
 	glClear(GL_COLOR_BUFFER_BIT);
 
@@ -57,6 +63,11 @@ void drawShapes()
 	t._color = {100, 200, 50};
 	t.draw();
 
+	/* Print/draw "Hello Kernel!". */
+	if (!ksplot_font_is_loaded(&font))
+		ksplot_print_text(&font, t._color.color_c_ptr(), 250, 70,
+				  "Hello Kernel!");
+
 	KsPlot::Rectangle r;
 	KsPlot::Point d(400, 200), e(400, 300), f(500, 300), g(500, 200);
 	r.setPoint(0, d);
Signature · 1 line
2.20.1
#4 lore raw

RFC PATCH v2 4/5 kernel-shark: Add hello_kernel plugin

In reply to Yordan Karadzhov (VMware)

This plugin is meant to be used for demonstration purposes only, or as part of a tutorial describing how to write a new plugin.

---
 kernel-shark/examples/CMakeLists.txt |  4 ++
 kernel-shark/examples/hello_kernel.c | 67 ++++++++++++++++++++++++++++
 kernel-shark/src/libkshark-plugin.h  |  3 ++
 3 files changed, 74 insertions(+)
 create mode 100644 kernel-shark/examples/hello_kernel.c
kernel-shark/examples/CMakeLists.txt +4 -0
index e16216e..824b21c 100644
@@ -23,3 +23,7 @@ target_link_libraries(dplot   kshark-plot)
 message(STATUS "widgetdemo")
 add_executable(widgetdemo          widgetdemo.cpp)
 target_link_libraries(widgetdemo   kshark-gui)
+
+add_library(hello             SHARED  hello_kernel.c)
+set_target_properties(hello   PROPERTIES PREFIX "plugin-")
+target_link_libraries(hello   kshark-plot)
kernel-shark/examples/hello_kernel.c new file +67 -0

67 lines not on this page · raw message

kernel-shark/src/libkshark-plugin.h +3 -0
index b3cf1c6..b182d79 100644
@@ -103,6 +103,9 @@ enum kshark_plugin_actions {
 	KSHARK_PLUGIN_CPU_DRAW,
 };
 
+/** No event identifier associated with the plugin. */
+#define KS_PLUGIN_NO_EVENT (-ENODEV)
+
 /**
  * Plugin Event handler structure, defining the properties of the required
  * kshark_entry.
Signature · 1 line
2.20.1
#5 lore raw

RFC PATCH v2 5/5 kernel-shark: Drop Freeglut as a compulsory dependency

In reply to Yordan Karadzhov (VMware)

Freeglut is not a necessary dependency for programs like the KernelShark GUI that are using Qt for window and OpenGL context management. Currently it is been used only by the data plotting example (dataplot.cpp).

---
 kernel-shark/build/deff.h.cmake      |  3 +++
 kernel-shark/examples/CMakeLists.txt | 10 +++++++---
 kernel-shark/src/CMakeLists.txt      |  4 ++--
 kernel-shark/src/KsGLWidget.cpp      |  4 ----
 kernel-shark/src/KsPlotTools.cpp     |  4 ----
 kernel-shark/src/libkshark-plot.c    | 17 ++++++++---------
 kernel-shark/src/libkshark-plot.h    |  2 +-
 7 files changed, 21 insertions(+), 23 deletions(-)
kernel-shark/build/deff.h.cmake +3 -0
index efee2a1..e3543ca 100644
@@ -23,6 +23,9 @@
 /** "pkexec" executable. */
 #cmakedefine DO_AS_ROOT "@DO_AS_ROOT@"
 
+/** GLUT has been found. */
+#cmakedefine GLUT_FOUND
+
 #ifdef __cplusplus
 
 	#include <QString>
kernel-shark/examples/CMakeLists.txt +7 -3
index 824b21c..74898c0 100644
@@ -16,9 +16,13 @@ message(STATUS "confogio")
 add_executable(confio          configio.c)
 target_link_libraries(confio   kshark)
 
-message(STATUS "dataplot")
-add_executable(dplot          dataplot.cpp)
-target_link_libraries(dplot   kshark-plot)
+if (GLUT_FOUND)
+
+    message(STATUS "dataplot")
+    add_executable(dplot          dataplot.cpp)
+    target_link_libraries(dplot   kshark-plot)
+
+endif (GLUT_FOUND)
 
 message(STATUS "widgetdemo")
 add_executable(widgetdemo          widgetdemo.cpp)
kernel-shark/src/CMakeLists.txt +2 -2
index 36ce884..8c8f988 100644
@@ -38,7 +38,7 @@ if (_DEVEL)
 
 endif (_DEVEL)
 
-if (OPENGL_FOUND AND GLUT_FOUND)
+if (OPENGL_FOUND)
 
     message(STATUS "libkshark-plot")
     add_library(kshark-plot  SHARED  libkshark-plot.c
@@ -65,7 +65,7 @@ if (OPENGL_FOUND AND GLUT_FOUND)
 
     endif (_DEVEL)
 
-endif (OPENGL_FOUND AND GLUT_FOUND)
+endif (OPENGL_FOUND)
 
 if (Qt5Widgets_FOUND AND Qt5Network_FOUND)
 
kernel-shark/src/KsGLWidget.cpp +0 -4
index ce68052..37819d3 100644
@@ -9,10 +9,6 @@
  *  @brief   OpenGL widget for plotting trace graphs.
  */
 
-// OpenGL
-#include <GL/glut.h>
-#include <GL/gl.h>
-
 // KernelShark
 #include "KsGLWidget.hpp"
 #include "KsUtils.hpp"
kernel-shark/src/KsPlotTools.cpp +0 -4
index a8eddcd..152ec47 100644
@@ -16,10 +16,6 @@
 #include <algorithm>
 #include <vector>
 
-// OpenGL
-#include <GL/freeglut.h>
-#include <GL/gl.h>
-
 // KernelShark
 #include "KsPlotTools.hpp"
 
kernel-shark/src/libkshark-plot.c +8 -9
index 1982494..32e2ae9 100644
@@ -19,6 +19,7 @@
 
 // KernelShark
 #include "libkshark-plot.h"
+#include "KsCmakeDef.hpp"
 
 /*
  * STB TrueType (single-file public domain library)
@@ -31,6 +32,10 @@
 #define STBTT_STATIC
 #include "stb_truetype.h"
 
+#ifdef GLUT_FOUND
+
+#include <GL/freeglut.h>
+
 /**
  * @brief Create an empty scene for drawing.
  *
@@ -55,17 +60,11 @@ void ksplot_make_scene(int width, int height)
 	/* Open the screen window. */
 	glutCreateWindow("KernelShark Plot");
 
-	/*
-	 * Set the origin of the coordinate system to be the top left corner.
-	 * The "Y" coordinate is inverted.
-	 */
-	gluOrtho2D(0, width, height, 0);
-	glViewport(0, 0, width, height);
-
-	glMatrixMode(GL_PROJECTION);
-	glLoadIdentity();
+	ksplot_resize_opengl(width, height);
 }
 
+#endif // GLUT_FOUND
+
 /**
  * @brief Initialize OpenGL.
  *
kernel-shark/src/libkshark-plot.h +1 -1
index 1895bee..2c44787 100644
@@ -16,8 +16,8 @@
 #include <stdbool.h>
 
 // OpenGL
-#include <GL/freeglut.h>
 #include <GL/gl.h>
+#include <GL/glu.h>
 
 /*
  * STB TrueType (single-file public domain library)
Signature · 1 line
2.20.1
#6 lore raw

Re: [RFC PATCH v2 5/5] kernel-shark: Drop Freeglut as a compulsory dependency

Quoted 18 lines · Yordan Karadzhov (VMware)

Freeglut is not a necessary dependency for programs like the KernelShark GUI that are using Qt for window and OpenGL context management. Currently it is been used only by the data plotting example (dataplot.cpp).

---
 kernel-shark/build/deff.h.cmake      |  3 +++
 kernel-shark/examples/CMakeLists.txt | 10 +++++++---
 kernel-shark/src/CMakeLists.txt      |  4 ++--
 kernel-shark/src/KsGLWidget.cpp      |  4 ----
 kernel-shark/src/KsPlotTools.cpp     |  4 ----
 kernel-shark/src/libkshark-plot.c    | 17 ++++++++---------
 kernel-shark/src/libkshark-plot.h    |  2 +-
 7 files changed, 21 insertions(+), 23 deletions(-)

[...]

Quoted 30 lines
kernel-shark/src/libkshark-plot.c +5 -3
index 1982494..32e2ae9 100644
@@ -19,6 +19,7 @@
 
 // KernelShark
 #include "libkshark-plot.h"
+#include "KsCmakeDef.hpp"
 
 /*
  * STB TrueType (single-file public domain library)
@@ -31,6 +32,10 @@
 #define STBTT_STATIC
 #include "stb_truetype.h"
 
+#ifdef GLUT_FOUND
+
+#include <GL/freeglut.h>
+
 /**
  * @brief Create an empty scene for drawing.
  *
@@ -55,17 +60,11 @@ void ksplot_make_scene(int width, int height)
        /* Open the screen window. */
        glutCreateWindow("KernelShark Plot");
 
-       /*
-        * Set the origin of the coordinate system to be the top left corner.
-        * The "Y" coordinate is inverted.

kernel-shark/src/libkshark-plot.c void ksplot_make_scene(int width, int height) · line 66

Quoted 10 lines
kernel-shark/src/libkshark-plot.c +2 -6
-        */
-       gluOrtho2D(0, width, height, 0);
-       glViewport(0, 0, width, height);
-
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
+       ksplot_resize_opengl(width, height);
 }
 
+#endif // GLUT_FOUND

I think we shouldn't link libkshark-plot with GLUT even if it's available on the system and let users of the library decide whether they need it. The glut code in this function is pretty standard GLUT initialization which users would want to control anyway and there's no point in providing it here.

Dropping GLUT as libkshark-plot dependency would imply GLU needs to be added as dependency for the gluOrtho2D() function (GLUT depends on GLU so that's how we've been picking up GLU till now). Alternatively, we can drop GLU altogether as dependency by implementing gluOrtho2D() ourselves (by using something like this[1] and glLoadMatrix() it).

[1] https://github.com/skaslev/catmull-clark/blob/e7142dd270176a38c180fd74b67da740ea68991c/mathx.c#L169

Cheers,

-- Slavi

Reviewed-by on the v2 series
#7 lore raw
In reply to Yordan Karadzhov (VMware)
Quoted 22 lines · Yordan Karadzhov (VMware)

Implemented using the single-file public domain library "stb_truetype":
https://github.com/nothings/stb/blob/master/stb_truetype.h

PS: Patch 1/3 is a simple fix suggested by Slavomir which strictly speaking is independent from the other two patches.

v2 changes:
- 2 new patches (patches 4/5 and 5/5).

- Added a procedure for finding a font file (patch 2/5).

- Addressed the comments made by Slavomir in his review of the previous version.

Yordan Karadzhov (VMware) (5):
kernel-shark: Simplify the drawing of the Triangle Fan
kernel-shark: Add support for drawing text
kernel-shark: Add text rendering to the plotting example
kernel-shark: Add hello_kernel plugin
kernel-shark: Drop Freeglut as a compulsory dependency

Quoted 15 lines
 kernel-shark/build/deff.h.cmake      |    3 +
 kernel-shark/doc/dox_config          |    1 +
 kernel-shark/examples/CMakeLists.txt |   14 +-
 kernel-shark/examples/dataplot.cpp   |   11 +
 kernel-shark/examples/hello_kernel.c |   67 +
 kernel-shark/src/CMakeLists.txt      |    4 +-
 kernel-shark/src/KsGLWidget.cpp      |    4 -
 kernel-shark/src/KsPlotTools.cpp     |    4 -
 kernel-shark/src/libkshark-plot.c    |  239 +-
 kernel-shark/src/libkshark-plot.h    |   46 +
 kernel-shark/src/libkshark-plugin.h  |    3 +
 kernel-shark/src/stb_truetype.h      | 4882 ++++++++++++++++++++++++++
 12 files changed, 5243 insertions(+), 35 deletions(-)
 create mode 100644 kernel-shark/examples/hello_kernel.c
 create mode 100644 kernel-shark/src/stb_truetype.h

For patches 1 to 4:

Cheers,

-- Slavi