From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org,
"Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Subject: [PATCH 2/5] kernel-shark-qt: Add basic instruments for OpenGL plotting.
Date: Wed, 22 Aug 2018 18:41:37 +0300 [thread overview]
Message-ID: <20180822154140.3748-3-y.karadz@gmail.com> (raw)
In-Reply-To: <20180822154140.3748-1-y.karadz@gmail.com>
This patch introduces tools for initializing OpenGL and for drawing
the three basic geometrical primitives (point, line and polygon)
used when plotting KernelShark's graphs.
Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
kernel-shark-qt/src/CMakeLists.txt | 13 ++
kernel-shark-qt/src/libkshark-plot.c | 212 +++++++++++++++++++++++++++
kernel-shark-qt/src/libkshark-plot.h | 69 +++++++++
3 files changed, 294 insertions(+)
create mode 100644 kernel-shark-qt/src/libkshark-plot.c
create mode 100644 kernel-shark-qt/src/libkshark-plot.h
diff --git a/kernel-shark-qt/src/CMakeLists.txt b/kernel-shark-qt/src/CMakeLists.txt
index a762da1..9c74bc0 100644
--- a/kernel-shark-qt/src/CMakeLists.txt
+++ b/kernel-shark-qt/src/CMakeLists.txt
@@ -13,5 +13,18 @@ target_link_libraries(kshark ${CMAKE_DL_LIBS}
set_target_properties(kshark PROPERTIES SUFFIX ".so.${KS_VERSION_STRING}")
+if (OPENGL_FOUND AND GLUT_FOUND)
+
+ message(STATUS "libkshark-plot")
+ add_library(kshark-plot SHARED libkshark-plot.c)
+
+ target_link_libraries(kshark-plot kshark
+ ${OPENGL_LIBRARIES}
+ ${GLUT_LIBRARY})
+
+ set_target_properties(kshark-plot PROPERTIES SUFFIX ".so.${KS_VERSION_STRING}")
+
+endif (OPENGL_FOUND AND GLUT_FOUND)
+
configure_file( ${KS_DIR}/build/deff.h.cmake
${KS_DIR}/src/KsDeff.h)
diff --git a/kernel-shark-qt/src/libkshark-plot.c b/kernel-shark-qt/src/libkshark-plot.c
new file mode 100644
index 0000000..17d3b90
--- /dev/null
+++ b/kernel-shark-qt/src/libkshark-plot.c
@@ -0,0 +1,212 @@
+/* SPDX-License-Identifier: LGPL-2.1 */
+
+/*
+ * Copyright (C) 2018 VMware Inc, Yordan Karadzhov <y.karadz@gmail.com>
+ */
+
+ /**
+ * @file libkshark-plot.c
+ * @brief Basic tools for OpenGL plotting.
+ */
+
+// OpenGL
+#include <GL/freeglut.h>
+#include <GL/gl.h>
+
+// KernelShark
+#include "libkshark-plot.h"
+
+/**
+ * @brief Create an empty scene for drawing.
+ *
+ * @param width: Width of the screen window in pixels.
+ * @param height: Height of the screen window in pixels.
+ */
+void ksplot_make_scene(int width, int height)
+{
+ /* Set Display mode. */
+ glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
+
+ /* Prevent the program from exiting when a window is closed. */
+ glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,
+ GLUT_ACTION_GLUTMAINLOOP_RETURNS);
+
+ /* Set window size. */
+ glutInitWindowSize(width, height);
+
+ /* Set window position on screen. */
+ glutInitWindowPosition(50, 50);
+
+ /* 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();
+}
+
+/**
+ * @brief Initialize OpenGL.
+ *
+ * @param dpr: Device Pixel Ratio.
+ */
+void ksplot_init_opengl(int dpr)
+{
+ glDisable(GL_TEXTURE_2D);
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_COLOR_MATERIAL);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable(GL_POLYGON_SMOOTH);
+ glLineWidth(1.5 * dpr);
+ glPointSize(2.5 * dpr);
+ glClearColor(1, 1, 1, 1);
+}
+
+/**
+ * @brief To be called whenever the OpenGL window has been resized.
+ *
+ * @param width: Width of the screen window in pixels.
+ * @param height: Height of the screen window in pixels.
+ */
+void ksplot_resize_opengl(int width, int height)
+{
+ glViewport(0, 0, width, height);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+
+ /*
+ * Set the origin of the coordinate system to be the top left corner.
+ * The "Y" coordinate is inverted.
+ */
+ gluOrtho2D(0, width, height, 0);
+
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+}
+
+/**
+ * @brief Draw a point.
+ *
+ * @param p: Input location for the point object.
+ * @param col: The color of the point.
+ * @param size: The size of the point.
+ */
+void ksplot_draw_point(const struct ksplot_point *p,
+ const struct ksplot_color *col,
+ float size)
+{
+ if (!p || !col || size < .5f)
+ return;
+
+ glPointSize(size);
+ glBegin(GL_POINTS);
+ glColor3ub(col->red, col->green, col->blue);
+ glVertex2i(p->x, p->y);
+ glEnd();
+}
+
+/**
+ * @brief Draw a line.
+ *
+ * @param a: Input location for the first finishing point of the line.
+ * @param b: Input location for the second finishing point of the line.
+ * @param col: The color of the line.
+ * @param size: The size of the line.
+ */
+void ksplot_draw_line(const struct ksplot_point *a,
+ const struct ksplot_point *b,
+ const struct ksplot_color *col,
+ float size)
+{
+ if (!a || !b || !col || size < .5f)
+ return;
+
+ glLineWidth(size);
+ glBegin(GL_LINES);
+ glColor3ub(col->red, col->green, col->blue);
+ glVertex2i(a->x, a->y);
+ glVertex2i(b->x, b->y);
+ glEnd();
+}
+
+/**
+ * @brief Draw a polygon.
+ *
+ * @param points: Input location for the array of points defining the polygon.
+ * @param n_points: The size of the array of points.
+ * @param col: The color of the polygon.
+ * @param size: The size of the polygon.
+ */
+void ksplot_draw_polygon(const struct ksplot_point *points,
+ size_t n_points,
+ const struct ksplot_color *col,
+ float size)
+{
+ if (!points || !n_points || !col || size < .5f)
+ return;
+
+ if (n_points == 1) {
+ ksplot_draw_point(points, col, size);
+ return;
+ }
+
+ if (n_points == 2) {
+ ksplot_draw_line(points, points + 1, col, size);
+ 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.
+ */
+ 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);
+
+ glVertex2i(points[0].x, points[0].y);
+ glEnd();
+}
+
+/**
+ * @brief Draw the contour of a polygon.
+ *
+ * @param points: Input location for the array of points defining the polygon.
+ * @param n_points: The size of the array of points.
+ * @param col: The color of the polygon.
+ * @param size: The size of the polygon.
+ */
+void ksplot_draw_polygon_contour(const struct ksplot_point *points,
+ size_t n_points,
+ const struct ksplot_color *col,
+ float size)
+{
+ if (!points || !n_points || !col || size < .5f)
+ return;
+
+ /* Loop over the points of the polygon and draw connecting lines. */
+ for(size_t i = 1; i < n_points; ++i)
+ ksplot_draw_line(&points[i - 1],
+ &points[i],
+ col,
+ size);
+
+ /* Close the contour. */
+ ksplot_draw_line(&points[0],
+ &points[n_points - 1],
+ col,
+ size);
+}
diff --git a/kernel-shark-qt/src/libkshark-plot.h b/kernel-shark-qt/src/libkshark-plot.h
new file mode 100644
index 0000000..9a4dbc0
--- /dev/null
+++ b/kernel-shark-qt/src/libkshark-plot.h
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: LGPL-2.1 */
+
+/*
+ * Copyright (C) 2018 VMware Inc, Yordan Karadzhov <y.karadz@gmail.com>
+ */
+
+ /**
+ * @file libkshark-plot.h
+ * @brief Basic tools for OpenGL plotting.
+ */
+
+#ifndef _LIB_KSHARK_PLOT_H
+#define _LIB_KSHARK_PLOT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Structure defining a RGB color. */
+struct ksplot_color {
+ /** The Red component of the color. */
+ uint8_t red;
+
+ /** The Green component of the color. */
+ uint8_t green;
+
+ /** The Blue component of the color. */
+ uint8_t blue;
+};
+
+/** Structure defining a 2D point. */
+struct ksplot_point {
+ /** The horizontal coordinate of the point in pixels. */
+ int x;
+
+ /** The vertical coordinate of the pointin in pixels. */
+ int y;
+};
+
+void ksplot_make_scene(int width, int height);
+
+void ksplot_init_opengl(int dpr);
+
+void ksplot_resize_opengl(int width, int height);
+
+void ksplot_draw_point(const struct ksplot_point *p,
+ const struct ksplot_color *col,
+ float size);
+
+void ksplot_draw_line(const struct ksplot_point *a,
+ const struct ksplot_point *b,
+ const struct ksplot_color *col,
+ float size);
+
+void ksplot_draw_polygon(const struct ksplot_point *points,
+ size_t n_points,
+ const struct ksplot_color *col,
+ float size);
+
+void ksplot_draw_polygon_contour(const struct ksplot_point *points,
+ size_t n_points,
+ const struct ksplot_color *col,
+ float size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--
2.17.1
next prev parent reply other threads:[~2018-08-22 19:07 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-22 15:41 [PATCH 0/5] Add drawing instrumentation to be used by the Qt-based KernelShark Yordan Karadzhov (VMware)
2018-08-22 15:41 ` [PATCH 1/5] kernel-shark-qt: Add OpenGL/GLUT as a third party dependency Yordan Karadzhov (VMware)
2018-08-22 15:41 ` Yordan Karadzhov (VMware) [this message]
2018-08-22 15:41 ` [PATCH 3/5] kernel-shark-qt: Add C++ API for drawing of Graphs Yordan Karadzhov (VMware)
2018-08-23 20:20 ` Matthew Helsley
2018-08-24 1:43 ` Steven Rostedt
2018-08-22 15:41 ` [PATCH 4/5] kernel-shark-qt: Add an example showing how to draw shapes and Graphs Yordan Karadzhov (VMware)
2018-08-22 15:41 ` [PATCH 5/5] kernel-shark-qt: Version 0.8.0 Yordan Karadzhov (VMware)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180822154140.3748-3-y.karadz@gmail.com \
--to=y.karadz@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.